[Tutor] Globals as variables in a tkinter widget

Alan Gauld alan.gauld at btinternet.com
Sat Dec 11 01:06:19 CET 2010


<echowit at aol.com> wrote

> This code produces a good run.

Not sure what you mean by that. What is "a good run"?

> from tkinter import *
> root = Tk()
> class Application(Frame):
>     global sv, Ptype, PtypeI, sel_rate_label, label

This is bizarre.
Globals are generally considered evil and to be avoided if possible.
One way to avoid globals is to use classes.
So why on earth are you trying to introduce globals ionto a class?
What do you think you will achieve that using class variables can't?

>     label = Label(root)
>     Ptype = 999
>    PtypeI = IntVar()
>    W = 5
>     Y = 4

And in fact most of these should probably be instance
variables defined within your init() method...


    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

Mixing print statements and GUIS is valid for debugging,
but these don't look like debnug statements... Are you sure
you want these appearing in the console rather than the GUI?

    def __init__(self,master=None):
        Frame.__init__(self)

You might want to pass the master in to Frame too...

        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()

Why use the global label here when you use the instance
level labels elsewhere? Especially as you don't appear to use it?

> 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'

It seems to think that Y is a string.
Your code suggests Y is V**2 or 4.
Can you insertt a print statement just before the offending
line to find out what W and Y are at that exact point?

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

Why do you think that will be useful?
What do you think you can do with globals that you can't do with
class or instance variables?

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

There are a lot of GUI tutorials, some even use Tkinter.
Even my tutorial includes the GUI in the case study and the event 
driven
programming topics as well as the GUI topic itself. They don;t go far
beyond hello world but they do take at least one step further.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list