[Tutor] Globals as variables in a tkinter widget

echowit at aol.com echowit at aol.com
Fri Dec 10 23:11:38 CET 2010



This code produces a good run.
 
from tkinter import *
root = Tk()
class Application(Frame):
    global sv, Ptype, PtypeI, sel_rate_label, label
    label = Label(root)
    Ptype = 999
    PtypeI = IntVar()
    W = 5
    Y = 4
    def sel(self):
        global W
        global Y
        Ptype = PtypeI.get()
        if Ptype <= 333: print('\nResulting Values:', PtypeI.get(),Ptype)
        else: print('Ptype Failure:',PtypeI.get())
        print('Ptype Completed',Ptype,'\n')
        V = 2
        X = 3
        Z = X * 2
        W = X + 6
        Y = V ** 2
        U = W * Y
        print("V: ",V," X: ",X," Z: ",Z," W: ",W," Y: ",Y," U: ",U,'\n')
        return
    
    def __init__(self,master=None):
        Frame.__init__(self)
        self.pack()
        sel_rate_label = Label(self)
        sel_rate_label.config(text = '\nSelect Goats Personality Type',)
        sel_rate_label.pack(anchor=N)
        MODES = [
            ("1 Below Average", 000), ("2 Average", 111),
            ("3 Above Average", 222), ("4 Super Star", 333),
        ]
        for text, mode in MODES:
            b = Radiobutton(self, text=text,
                        variable=PtypeI, value=mode, command = self.sel)
            b.pack(anchor=W)
        label.pack()
        return
app = Application(master=root) 
app.mainloop()
#root.destroy()

This is the IDLE output.
 
>>> ================================ RESTART ================================
>>> 
Resulting Value: 333 333 
Ptype Completed 333 
V:  2  X:  3  Z:  6  W:  9  Y:  4  U:  36 
>>> 

Assigning U prior to recalculating W & Y:
 
        U = W * Y
        V = 2
        X = 3
        Z = X * 2
        W = X + 6
        Y = V ** 2
        print("V: ",V," X: ",X," Z: ",Z," W: ",W," Y: ",Y," U: ",U,'\n')

Produces:

>>> ================================ RESTART ================================
>>> 
Resulting Values: 333 333
Ptype Completed 333 
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python31\lib\tkinter\__init__.py", line 1399, in __call__
    return self.func(*args)
  File "C:\Larry\FCGCF\GPDB\Dev\EX_OK.py", line 18, in sel
    U = W * Y
TypeError: can't multiply sequence by non-int of type 'str'

My goal is to I learnto use a global variable as an argument inside a widget function,

My Question is: Anybody know of some good on-line documentation about using GUIs to do more than say 'Hello World'? 

Thanx in advance,

Larry Rochester
AKA echowit at aol.com


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101210/2cf36ea9/attachment-0001.html>


More information about the Tutor mailing list