[Tkinter-discuss] calling toplevel creating function

Michael Lange klappnase at web.de
Wed May 31 00:32:26 CEST 2006


On Wed, 31 May 2006 00:00:32 +0300
"Ilknur Ozturk" <Ilknur.Ozturk at cabot.com.tr> wrote:

> RE: [Tkinter-discuss] calling toplevel creating function
> 
> Hi Michael,
> 
> my code is below. actually the problem that I explained in my previous mail can be solved by using newkey() instead of self.newkey() toplevel definition.(but still could not understand what is the reason of the error message)

Hi Ilknur,

self.newkey is the name of the method that creates the Toplevel window, if you name the window self.newkey, too,
you override the method. Now when in your self.Button_Press() method the call to self.newkey() occurs,
you actually try to "call" the newly created Toplevel window which leads to the error message you saw.



 however, still I could not reach my aim. As you will see below, I could not assign self.userkey which is gotten from the user on toplevel window, to the userdefinedkey[] array. the aim is, at first click of a key should result with a toplevel window that gets the key parameter from the user. the other press of the same key should result by calling a different function with the parameter taken. there are more than one such a key on my GUI. I hope, I can explain my aim and problems:) thanks from now...

The problem here seems similar, if I understand you correctly. Each call to self.newkey() overrides a bunch of
variable names.
I think a better approach would be to write a separate class for your dialog window; if I understand the use
of your dialog window correctly, it is probably the best (and easiest) to use an askstring() dialog from the
tkSimpleDialog module, e.g.:

    (...)
    # do not forget to make the list a class attribute first
    self.userdefinedkeys = ['', '']
    # define the buttons like this:
    self.key1 = Button(self.mycontainer, text="Key1", command=lambda index=0 : self.button_press(index))
    (...)

    def button_press(self, index):
        if self.userdefinedkeys[index]:
            # alternative callback here
            print self.userdefinedkeys[index]
        else:
            self.userdefinedkeys[index] = tkSimpleDialog.askstring("Enter new key", "Key name:")

I hope this helps

Michael

> 
> 
> 
> from Tkinter import *
> 
> root = Tk()
> 
> class myentryclass:
>     def __init__(self, parent):
>         self.myParent = parent
>         self.myParent.focus_set()
>        
>         self.mycontainer = Frame(parent, borderwidth=0)
>         self.mycontainer.pack()
> 
> 
>         userdefinedkeys=[]
>         for i in range (2):
>             userdefinedkeys.append('')
> 
>         button_name=self.userdefinedkeys[0]
>         button_number=1
>         self.key1=Button(self.mycontainer,
>                            command=lambda
>                             arg1=button_name, arg2=button_number:
>                             self.Button_Press(arg1, arg2)
>                            )
>         self.key1.configure(text="key1")
>         self.key1.grid(row=3, column=0)
> 
>         button_name=self.userdefinedkeys[1]
>         button_number=2
>         self.key2=Button(self.mycontainer,
>                            command=lambda
>                             arg1=button_name, arg2=button_number:
>                             self.Button_Press(arg1, arg2)
>                            )
>         self.key2.configure(text="key2")
>         self.key2.grid(row=3, column=1)
> 
> 
>     def Button_Press(self, arg1, arg2):
>         if arg2==1:
>             if arg1=='':
>                 self.newkey()
>                 self.userdefinedkeys[0]=self.userkey
>             else:
>                 print self.userdefinedkeys[0]
>         if arg2==2:
>             if arg1=='':
>                 self.newkey()
>                 self.userdefinedkeys[1]=self.userkey
>             else:
>                 print self.userdefinedkeys[1]
> 
>         else:
>             pass
>     
>     def newkey(self):
>             newkey=Toplevel()
>             newkey.title('Define New Key')
>             newkey.geometry('250x140+500+300')
> 
>             self.keydef = Label(newkey, text="Key Name:")
>             self.keydef.place(x=10, y=10)
>             self.keyname = Entry(newkey)
>             self.keyname.focus_set()
>             self.keyname.place(x=90, y=10)
>             confirm = Button(newkey, text="OK", width=15, command=self.getkeyname)
>             confirm.place(x=67, y=85)
>            
>     def getkeyname(self):
>         self.userkey=self.keyname.get()
> ##        self.newkey.destroy()
>         print self.userkey
>         return self.userkey        
> 
>   
>     def sendcommand(self, arg1):
>         print arg1
> 
> 
> mylittlentry = myentryclass(root)
> root.mainloop()
> 
> 


More information about the Tkinter-discuss mailing list