Title: Signature.html
I see my post of yesterday, "Maintaining the Same Variable Type--Tkinter", went over like a lead balloon. :-)
(Note though the Myth Busters TV series successfully built and flew a lead balloon.)

Let's see if I can really simplify what I'm after. I've pulled the essentials out of the original larger program I'm trying to modify. 2000 lines down to 80. I've only added a few lines of my own, and changed the menu and dialog content. Although I've heavily modified the original code to accept a config file, and set up variable to initialize the widgets, trying to bridge some code in the Enter_Data_Dialog and Set_Enter_Data objects control variable code is where the trouble lays for completing the modifications.

The code below puts up a window with one menu and two submenus items,  Enter Data, and Exit. Select Enter Data and put in an integer number. It all works fine. Basically, what I want to do is eliminate code like dialog.anumberVar.get() and replace it by constructing the appropriate names for the config file. In this case, the config file might contain:
    anumber = 123

Comments?

BTW, the Quit function is original but doesn't kill the window when Quit is used. What fixes that? For more bonus points, it seems as though the try statement in the dialog should really bring up an "Error" dialog saying something is wrong, when an invalid entry occurs. No need to construct an error dialog for me, but I'd be curious how it might be handled.


=================fun-intVar.py===========================
from Tkinter import *
import tkSimpleDialog
import tkMessageBox

class IntVar_GUI:

    def __init__(self, master):

        master.title('Control Variable Fun')

        self.frame = Frame(master,takefocus=1,
                           highlightthickness=2, highlightcolor='blue')
        self.frame.configure(height=200,width=200)
        self.frame.pack()
        #self.frame.bind("<KeyPress>", self.HandleKey)

        self.anumber = 123      # Want name and value to be configurable

        self.master = master
        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 = {}
        sdict[ "ok" ] = False
        sdict[ "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(eval(dialog.anumberVar.get()))
            print "OK"
        except:
            print "Not OK"
            pass
        print "self.anumber:", self.anumber

    def Quit(self):
        self.running = False
        self.master.quit()

    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).grid(row=0, column=1)
        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()
--
           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

             (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)
            

                In mathematics you don't understand things. 
                 You just get used to them.” -- John Von Neumann
                    (P.S. The same is true in life.)

                    Web Page: <www.speckledwithstars.net/>
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to