(Tkinter) Adding delay to PopUpMsg

H

Harlin Seritt

I am working on making something called a PopMsg widget which is
actually identical to a Balloon widget from Pmw. Here is the code:

---code---
from Tkinter import *
import time

class PopMsg:

def showmsg(self, event):
for a in range(10000000): pass
self.t.deiconify()
self.t.geometry(str(self.msg.winfo_reqwidth())+'x'+str(self.msg.winfo_reqheight())+'+'+str(event.x_root)+'+'+str(event.y_root))
self.widget.bind('<Leave>', self.hide)
time.sleep(0.001)

def hide(self, event):
self.t.withdraw()

def __init__(self, parent, widget, text=None):

self.parent=parent
self.widget=widget
self.text=text
self.t = Toplevel()
self.t.withdraw()
self.t.overrideredirect(1)
self.msg = Message(self.t
, text=self.text
, bg='white'
, border=1
, relief=SOLID
, aspect=250)
self.msg.pack(ipadx=10, ipady=5)
self.parent.unbind('<Enter>')
self.parent.focus()
self.widget.bind('<Enter>', self.showmsg)


def printhello():
print 'Hello'

root = Tk()
button = Button(root, text='Hello !', command=printhello)
button.pack(padx=10, pady=10)
p = PopMsg(root, button, text='Some messaging text can go here.')

root.mainloop()

---End Code---

My problem you'll notice is that I tried to put something of a 'delay'
that will allow the popup to show a few seconds after the cursor has
entered the targeted widget's space (in this case the button). That
works ok but I've found that while waiting I can't activate (or
'click') on the button until the PopMsg has shown up. Is there anything
I can do to solve this problem?

thanks,

Harlin Seritt
 
E

Eric Brunel

I am working on making something called a PopMsg widget which is
actually identical to a Balloon widget from Pmw. Here is the code:

---code---
from Tkinter import *
import time

class PopMsg:

def showmsg(self, event):
for a in range(10000000): pass

Very effective way to waste a lot of CPU time and to make your delay totally dependent on the CPU speed! Why don't you at least use time.sleep (that you use some lines below for no apparent reason BTW)?

[snip code]
My problem you'll notice is that I tried to put something of a 'delay'
that will allow the popup to show a few seconds after the cursor has
entered the targeted widget's space (in this case the button). That
works ok but I've found that while waiting I can't activate (or
'click') on the button until the PopMsg has shown up. Is there anything
I can do to solve this problem?

Use Tkinter widgets' "after" method; the binding on <Enter> on the widget should do something like:

self.widget.after(1000, self.showmsg)

This tells the tk main loop to wait 1000 milliseconds, then run the method. Note that no event will be passed, so you should make the "event" parameter in showmsg optional. This is an active wait, so all other events will be treated. If for some reason, you want to cancel the action, you can remember the result of the "after" method, and then pass it to the after_cancel method on the same widget:

x = self.widget.after(1000, self.showmsg)
....
self.widget.after_cancel(x)

HTH
 

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

Members online

No members online now.

Forum statistics

Threads
473,796
Messages
2,569,645
Members
45,369
Latest member
Carmen32T6

Latest Threads

Top