Tkinter event question

  • Thread starter Russell E. Owen
  • Start date

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()
 
Ad

Advertisements

E

Eric Brunel

Russell said:
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>>")

Do you really need to send the event to the label itself here? Apparently, it is
what causes the problem: doing a self.master.event_generate("<<Foo>>") has the
expected result.

Anyway, this is more a tcl/tk bug than a Python/Tkinter bug. If you know enough
tcl, you may try to report the problem to comp.lang.tcl.

HTH
 
Ad

Advertisements

R

Russell E. Owen

#!/usr/local/bin/python
from Tkinter import *
root = Tk()

class myLabel(Label):
def sendFoo(self):
self.event_generate("<<Foo>>")

Do you really need to send the event to the label itself here? Apparently, it
is
what causes the problem: doing a self.master.event_generate("<<Foo>>") has the
expected result.[/QUOTE]

That sounds like a good workaround. Thanks!

What I'm actually trying to do is:
- I have a "manage panels" widget that takes a list of control panel
widgets, displays a column of checkbuttons (one per widget) and then
either displays or hides each control panel depending on its associated
checkbutton
- In certain limited cases I want a control panel to open itself (with
the associated checkbox toggling appropriately)
- I was sending "<<ShowMe>>" from the panel to accomplish this, and it
only worked if the panel had ever been shown.

I was considering using a logical variable, but felt it was
inappropriately tight coupling between the "manage panels" widget and
the panels themselves.

I will verify that it's a tcl/tk bug and report it if so (and I agree it
seems virtually certain to be so).

-- Russell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top