[Tutor] trying to put Tkinter widget handlers in a module

dwbarne at earthlink.net dwbarne at earthlink.net
Mon Sep 29 03:05:48 CEST 2008


I am writing a large Python/Tkinter/Pmw program. It has become so big that I would like to move some of the widget handlers to a module for import. The following small program illustrates:

# --- begin code ---

# checkbutton frame using python & Tkinter

from Tkinter import *
import string

class CheckButton_1(Frame):
    def __init__(self,msg):
        Frame.__init__(self,master)
        self.grid(
            )
            
        self.createWidgets()
        
    def createWidgets(self):             
        self.var = IntVar()       
        c = Checkbutton(
            master, 
            text='Check if yes',
            variable=self.var,
            command=self.handlerCheckButton,
            )
        c.grid(
            )
            
    def handlerCheckButton(self):
        self.doNotSend=self.var.get()
        if self.doNotSend:
            print "\nChecked"
        else:
            print "\nNot checked"
            
if __name__ == "__main__":
    master=Tk()
    master.title("Checkbutton test")
    msg='If checked, do NOT send me a copy of this email'
    check=CheckButton_1(msg)
    check.mainloop()

# --- end code ---

If the method 'handlerCheckButton(self)' is moved to a module, them imported, the button may come up but once the Checkbutton is checked, the code crashes with an error. 

Is there no way to put handlers in a module and import them? Is 'self' getting in the way?

Daniel B.


More information about the Tutor mailing list