A Dangling Tk Entry

r rt8396 at gmail.com
Tue Mar 10 09:02:11 EDT 2009


OK, here is a slightly cleaned up version of this horrible code. I did
not change too much at one time for fear of confusing you. The main
problem is you did not explicitly grid the entry like i told you
earlier, and why you are using eval is beyond any measure of sanity...

from Tkinter import *
import tkSimpleDialog

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

     def Set_Enter_Data(self):
         sdict = {"ok":False, "anumber":self.anumber}
         dialog = Enter_Data_Dialog(self.master, sdict)
         self.Focus()
         print "Howdy, set data. Number is:", dialog.anumberVar.get()
         print "dict:", dialog.sdict
         if not dialog.sdict["ok"]:
             return
         try:
             self.anumber = int(dialog.anumberVar.get())#why the heck
where you using eval here?
             print "OK"
         except:
             print "Not OK"
             pass
         print "self.anumber:", self.anumber
     def Quit(self):
         self.running = False
         #self.master.quit()
         self.master.destroy()
     def Focus( self ):
         self.frame.focus_set()

class Enter_Data_Dialog(tkSimpleDialog.Dialog):
     def __init__(self, parent, sdict):
         self.sdict = sdict
         tkSimpleDialog.Dialog.__init__(self, parent)
     def body(self,master):
         self.title("Set a Number Entry Dialog")
         Label( master, text="Number ").grid(row=0, sticky=W)
         self.anumberVar = StringVar()
         entry = Entry(master, width=10, textvariable=self.anumberVar)
         entry.grid(row=0, column=1) #i told you to explicitly grid a
widget you want to call later
         entry.insert(0,11)
         self.anumberVar.set( "%d" % self.sdict["anumber"] )
         return entry
     def apply(self):
         self.sdict["ok"] = True

def Process():
     root = Tk()
     app = IntVar_GUI(root)
     root.mainloop()

if __name__ == "__main__":
     Process()

The next message i send will be a rewrite of this code in a proper
Pythonic fashion, this frankly is a plate of spaghetti!



More information about the Python-list mailing list