[Tkinter-discuss] Make GUI

Bob Greschke bob at passcal.nmt.edu
Fri Mar 30 20:15:29 CEST 2007


#! /bin/env python2.3

from Tkinter import *
from sys import exit

Root = Tk()

InputVar = StringVar()
OutputVar = StringVar()


######################
# BEGIN: class Command
# Pass arguments to functions from button presses and menu  
selections! Nice!
# In your declaration:  ...command = Command(func, args,...)
# Also use in bind() statements
#   x.bind("<****>", Command(func, args...))
class Command:
     def __init__(self, func, *args, **kw):
         self.func = func
         self.args = args
         self.kw = kw
     def __call__(self, *args, **kw):
         args = self.args+args
         kw.update(self.kw)
         apply(self.func, args, kw)
# END: class Command




###############################
# BEGIN: XSqCalc(InVar, OutVar)
def XSqCalc(InVar, OutVar):
     Value = InVar.get().strip()
     if Value == "":
         OutVar.set("error")
         return
     Value = float(Value)
     Value = Value*Value
     OutVar.set(str(Value))
     return
# END: def XSqCalc




Label(Root, text = "Enter a value in the field\non the left, click the 
\n\"X^2\"
button and see the\nanswer in the field on the right.").pack(side = TOP)
Sub = Frame(Root)
Entry(Sub, width = 10, textvariable = InputVar, bg = "white", \
         fg = "black").pack(side = LEFT)
Label(Sub, text = " ").pack(side = LEFT)
Button(Sub, text = "X^2", command = Command(XSqCalc, InputVar, \
         OutputVar)).pack(side = LEFT)
Label(Sub, text = " ").pack(side = LEFT)
Entry(Sub, width = 10, textvariable = OutputVar, bg = "white", \
         fg = "black").pack(side = LEFT)
Sub.pack(side = TOP, padx = 5, pady = 5)
Button(Root, text = "Quit", command = exit).pack(side = TOP)

Root.mainloop()


That python2.3 line at the beginning may need to be changed for your  
system.
The text of that first Label() should all be on the same line.

Bob

On Mar 30, 2007, at 11:35, Vinu Vikram wrote:

> Dear All
> I wrote a python script which will take  values from a  
> configuration file and perform some operation and give an answer. I  
> would like to make a gui for the script using Tkinter. Could  
> anybody help me to do that? Suppose my script use a variable 'x' to  
> calculate x^2. I would like to give the value x=2 through a GUI and  
> get the output in the same window. Please help me to find the  
> solution.
> Thanking You
> Vinu V.
> -- 
> VINU VIKRAM
> http://iucaa.ernet.in/~vvinuv/
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss



More information about the Tkinter-discuss mailing list