[Tutor] Problem with IntVar() - Oops! Here's the code.

Kent Johnson kent37 at tds.net
Sat Mar 5 05:48:01 CET 2005


There seem to be quite a few problems with this code...

Ronnie Betzen wrote:
> #! /usr/bin/env python
> import Tkinter
should be
   from Tkinter import *
> 
> class timer:
>   def __init__(self):
>       

The next line needs to be after the line 'self.top = Tk()' so Tkinter is initialized when the 
variable is created.
>       # initialize global variables
>       self.ckbuttonstatus = IntVar()
>       
>       # set up main window
>       self.top = Tk()
>       self.top.geometry('355x125')

appname is not defined...
>       self.top.title(appname)

etc...

I think you have bitten off too much here. Try starting with a very small, working program that has 
a little of what you want in it. Then add another little piece and get that working. Repeat until 
you get to where you want to be. Ask questions when you don't understand the next step.

Here is a very simple program that links a checkbox and a label through a variable. Maybe this is a 
step in the right direction for you:

from Tkinter import *

root = Tk()
var = IntVar()
Label(textvariable=var).pack()
c = Checkbutton(text="Click Me", variable=var)
c.pack()
root.mainloop()

Kent

>       self.top.resizable(width='no', height='no')
>       
>       # set up menubar
>       self.menubar = menu(self.top)
>       self.top.config(menu = self.menubar)
>       
>       # set up file menu
>       self.filemenu = Menu(self.menubar, tearoff = 0)
>       self.filemenu.add_cascade(label="File", menu=self.filemenu)
>       self.filemenu.add_checkbutton(label = "Click Me!", command = 
> self.labelUpdate)
>       self.filemenu.add_command(label = "Quit", command=self.top.quit)
>       
>       # set up main display area
>       self.frame = Frame(self.top)
>       self.frame.pack(fill=x)
>       self.displaylabel=Label(self.frame, text=" ", font="Times 36 bold", 
> relief="sunken")
>       self.displaylabel.pack(side=BOTTOM, fill=X)
>   
>   def labelUpdate(self):
>       self.status = self.ckbuttonstatus.get()
>       self.labeltext = "Chekbutton status is: " + self.status
>       self.displaylabel.config(text=labeltext)
>       self.top.update()
>       
> if __name__ == '__main__':
>      mainApp = timer()
>      mainloop()
>      
> 
> On Friday 04 March 2005 08:39 pm, Ronnie Betzen wrote:
> 
>>I've been trying to use IntVar() to update checkboxes using the Tkinter
>>toolkit.  To date I haven't been successful getting it to work.  Evidently
>>I'm missing some concept with regards to setting up my interface.  I've
>>successfully run scripts by other people using IntVar() so I know my
>>personal system is ok.
>>
>>Here is some code that causes me problems with IntVar().  Can someone
>>enlighten this total newbie (python and programming both.) as to what I'm
>>missing here?
>>
>>Thanks,
>>
>>Ron
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




More information about the Tutor mailing list