R
Russell E. Owen
I've found a quirk in Tkinter and am wondering if anyone knows a
workaround. I've got some widgets that generate events. Sometimes the
widgets are not displayed, but they may still want to generate these
events.
The problem is, if a widget has never been displayed, event_generate
appears to be ignored. If I display the widget and then hide it again,
event_generate works fine. But...I've got many widgets that are normally
hidden and don't really want the screen flashing with stuff being
displayed and hidden again right away as the application starts up.
Here is a minimal and unrealistic script that shows the problem. Push
the button to generate a <<Foo>> event. The event is actually only
generated if the label that generates it is shown (you can then hide it
again and the events are still generated). Note that the label *is*
initially gridded, but it's taken hidden again before the window manager
ever actually gets to display it.
Suggestions? This is driving me mad, as such events could be quite
useful for communication if I could only generate them reliably.
-- Russell
#!/usr/local/bin/python
from Tkinter import *
root = Tk()
class myLabel(Label):
def sendFoo(self):
self.event_generate("<<Foo>>")
def gotFoo(evt):
print "Got <<Foo>>"
root.bind("<<Foo>>", gotFoo)
c = myLabel(text="I Am A Label")
b = Button(root, text="Send <<Foo>>", command=c.sendFoo)
c.grid(row=0, column=0)
c.grid_remove()
b.grid(row=1, column=0)
showVar = IntVar()
def showFoo():
if showVar.get():
c.grid()
else:
c.grid_remove()
showC = Checkbutton(root, text="Show Label",
variable=showVar, command=showFoo)
showC.grid(row=2, column=0)
root.mainloop()
workaround. I've got some widgets that generate events. Sometimes the
widgets are not displayed, but they may still want to generate these
events.
The problem is, if a widget has never been displayed, event_generate
appears to be ignored. If I display the widget and then hide it again,
event_generate works fine. But...I've got many widgets that are normally
hidden and don't really want the screen flashing with stuff being
displayed and hidden again right away as the application starts up.
Here is a minimal and unrealistic script that shows the problem. Push
the button to generate a <<Foo>> event. The event is actually only
generated if the label that generates it is shown (you can then hide it
again and the events are still generated). Note that the label *is*
initially gridded, but it's taken hidden again before the window manager
ever actually gets to display it.
Suggestions? This is driving me mad, as such events could be quite
useful for communication if I could only generate them reliably.
-- Russell
#!/usr/local/bin/python
from Tkinter import *
root = Tk()
class myLabel(Label):
def sendFoo(self):
self.event_generate("<<Foo>>")
def gotFoo(evt):
print "Got <<Foo>>"
root.bind("<<Foo>>", gotFoo)
c = myLabel(text="I Am A Label")
b = Button(root, text="Send <<Foo>>", command=c.sendFoo)
c.grid(row=0, column=0)
c.grid_remove()
b.grid(row=1, column=0)
showVar = IntVar()
def showFoo():
if showVar.get():
c.grid()
else:
c.grid_remove()
showC = Checkbutton(root, text="Show Label",
variable=showVar, command=showFoo)
showC.grid(row=2, column=0)
root.mainloop()