A Dangling Tk Entry

r rt8396 at gmail.com
Tue Mar 10 14:58:01 CET 2009


Alright try this code. The only thing that i left out was inheriting
from Frame, but since i cannot see your entire progam i did not want
to do that for fear of breaking some other code. You had a lot of
useless code in there and more code that was just a spaghetti mess. If
you want to return a dict instead of a tuple like i did feel free :)




from Tkinter import *
import tkSimpleDialog

class EnterDataDialog(tkSimpleDialog.Dialog):
    def __init__(self, parent, sdict):
        self.sdict = sdict
        tkSimpleDialog.Dialog.__init__(self, parent)
    def body(self, master):
        self.title("Set a Number")
        Label( master, text="Number ").grid(row=0, sticky=W)
        self.v = IntVar()
        entry = Entry(master, width=10, textvariable=self.v)
        entry.grid(row=0, column=1) #i told you to explicitly grid a
widget you want to call later
        self.v.set( "%d" % self.sdict["anumber"] )
        return entry #return widget that recieves focus
    def validate(self):
        try:
            self.v.get()
        except:
            return 0
        return 1
    def apply(self):
        self.result = self.v.get()

class IntVar_GUI():
    def __init__(self, master):
        self.master = master
        master.title('Control Variable Fun')
        frame = Frame(master, height=200, width=200, takefocus=1,
highlightthickness=2, highlightcolor='blue')
        frame.pack()
        self.anumber = 123 # Want name and value to be configurable
        menu = Menu(master)
        master.config(menu=menu)
        mainMenu = Menu(menu)
        menu.add_cascade(label="My Menu", menu=mainMenu)
        mainMenu.add_command(label="Enter Data",
command=self.Set_Enter_Data)
        self.master.protocol("WM_DELETE_WINDOW", self.onQuit)
        frame.focus_set()

    def Set_Enter_Data(self):
        sdict = {"anumber":self.anumber}
        d = EnterDataDialog(self.master, sdict)
        if d.result:
            print "User Entered: ", d.result
        else:
            print 'The idiot pressed cancel!'

    def onQuit(self):
        self.running = False
        self.master.destroy()


if __name__ == "__main__":
     root = Tk()
     app = IntVar_GUI(root)
     root.mainloop()




More information about the Python-list mailing list