Hi,

Thanks for your reply. It wasn't lazyness, believe me, I did look around for 
solutions and found something similar to yours here:

http://www.astro.washington.edu/owen/TkinterSummary.html#TracingVariables

The problem is that I have a class and it wants the callback function to be 
a global name (I also can't figure out hot to pass parameters to it)

By being a global name I can't access the var with widget.getvar(), and it 
complains if I use root by saying:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python24\Lib\lib-tk\Tkinter.py", line 1345, in __call__
    return self.func(*args)
  File "C:\Workplace\python\piim\Tkclient.py", line 312, in doLangChoices
    varValue = root.getvar(name)
AttributeError: Tkclient instance has no attribute 'getvar'


The code on the class follows:

class StartWin:

        def initialize(self):
                self.startw=Tk(className="Login")
                self.startw.servlab=Label(self.startw, text="Server:")
                self.startw.servin=Entry(self.startw, width=15)
                self.startw.servlab.grid(column=0, row=0)
                self.startw.servin.grid(column=1, row=0)
                self.startw.portlab=Label(self.startw, text="Port:")
                self.startw.portin=Entry(self.startw, width=15)
                self.startw.portlab.grid(column=0, row=1)
                self.startw.portin.grid(column=1, row=1)
                self.startw.nicklab=Label(self.startw, text="Nickname:")
                self.startw.nickin=Entry(self.startw, width=15)
                self.startw.nicklab.grid(column=0, row=2)
                self.startw.nickin.grid(column=1, row=2)
                self.startw.l1=Label(self.startw, text="Language:")
                self.startw.l1.grid(column=0, row=3)
                                OPTIONS = ["en","pt","es","it","fr","de"]
                                self.startw.variable = StringVar()
                                self.startw.variable.set(OPTIONS[0]) # 
default value
                                self.startw.variable.trace('w', 
doLangChoices) # trace choices
                        self.startw.whis_butt=apply(OptionMenu, (self.startw, 
self.startw.variable) + tuple(OPTIONS))
                self.startw.whis_butt.grid(column=1, row=3)

                self.startw.login_butt=Button(self.startw, text="Login", 
command=self.login)
                self.startw.login_butt.grid(column=0, row=4)
                self.startw.exit_butt=Button(self.startw, text="Exit", 
command=self.exit)
                self.startw.exit_butt.grid(column=1, row=4)
                self.startw.mainloop()

        #connect client with inserted data
        def login(self):
                start_client(self.startw.servin.get(), 
int(self.startw.portin.get()), 
self.startw.nickin.get(), self.startw.variable.get())

        def exit(self):
                self.startw.quit()
                self.startw.destroy()
                #sys.exit()


def doLangChoices(name, index, mode):
        print "callback called with name=%r, index=%r, mode=%r" % (name, 
index, mode)
        # HOW TO ACCESS THE VAR VALUE HERE??
        #varValue = root.getvar(name)
        #print varValue

print "Client Started"
try:
    conn=StartWin()
    conn.initialize()
    root=Tkclient()
    thread.start_new_thread(listen_server, (client, root))
    root.startWindow()
except Exception:
    print "Client thread aborted"



>From: geon <[EMAIL PROTECTED]>
>To: Jorge Louis De Castro <[EMAIL PROTECTED]>
>CC: tutor@python.org
>Subject: Re: [Tutor] Event handler for optionmenu
>Date: Mon, 15 Aug 2005 11:41:30 +0200
>
>Jorge Louis De Castro napsal(a):
>
>>Hi,
>>  I have an optionmenu widget that works just fine except that  can't find 
>>docs to get hold of events as they happen, kinda like the command=XYZ of 
>>other widgets like buttons.
>>  The code I'm using for the option menu is the following:
>>  OPTIONS = ["en","pt","es","it","fr","de"]
>>self.startw.variable = StringVar()
>>self.startw.variable.set(OPTIONS[0]) # default value
>>self.startw.whis_butt=apply(OptionMenu, (self.startw, 
>>self.startw.variable) + tuple(OPTIONS))
>>self.startw.whis_butt.grid(column=1, row=3)
>>  Now I'd like to have a function like:
>>  def onChange(self):
>>      # do optionmenu specific stuff
>>  but can't find how to do it, how to associate/bind onChange with the 
>>optionmenu widget.
>>Any help will be highly appreciated.
>>
>
>This art is called Trace Variable and could be used like this (copied and 
>rearranged from one of the prevously posted message from here):
>
>from Tkinter import *
>
>OPTIONS = [
>    "egg",
>    "bunny",
>    "chicken"
>]
>
>def callbackFunc(name, index, mode):
>  #print "callback called with name=%r, index=%r, mode=%r" % (name, index, 
>mode)
>  varValue = root.getvar(name)
>  print varValue
>  # modify the value, just to show it can be done
>  #root.setvar(name, varValue)
>
>root = Tk()
>var = StringVar()
>var.set(OPTIONS[2]) # default value
>rememberMe = var.trace_variable('w', callbackFunc)  # var.trace('w', 
>callbackFunc) # this works, too
>
>
>
>w = OptionMenu (root, var, *OPTIONS)
>w.pack()
>
>
>root.mainloop()
>
>Hope its what you asked for :-)
>
>--
>geon


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to