Simple TK Question - refreshing the canvas when not in focus

blaine frikker at gmail.com
Tue Apr 29 11:09:18 EDT 2008


On Apr 29, 10:36 am, "Eric Brunel" <see.signat... at no.spam> wrote:
> On Tue, 29 Apr 2008 15:22:12 +0200, blaine <frik... at gmail.com> wrote:
> > Hey everyone!
> >   I'm not very good with Tk, and I am using a very simple canvas to
> > draw some pictures (this relates to that nokia screen emulator I had a
> > post about a few days ago).
>
> > Anyway, all is well, except one thing.  When I am not in the program,
> > and the program receives a draw command (from a FIFO pipe), the canvas
> > does not refresh until I click into the program. How do I force it to
> > refresh, or force the window to gain focus?  It seems like pretty
> > common behavior, but a few things that I've tried have not worked.
>
> > Class screen():
> >     def __init__(self):
> >         self.root = Tkinter.Tk()
> >         self.root.title('Nokia Canvas')
> >         self.canvas = Tkinter.Canvas(self.root, width =130,
> > height=130)
> >         self.canvas.pack()
> >         self.root.mainloop()
>
> > Then somewhere a long the line I do:
> >             self.canvas.create_line(args[0], args[1], args[2],
> > args[3], fill=color)
> >             self.canvas.pack()
>
> Unrelated question: why are you doing a .pack() again here? Packing the
> widget just inserts it at the right place in its container, so you only
> have to do it once. So the one you did in __init__ is enough.
>
> > I've tried self.root.set_focus(), self.root.force_focus(),
> > self.canvas.update(), etc. but I can't get it.
>
> IIRC:
> - self.root.set_focus() will only work if your application already has the
> focus, so it's not what you need here.
> - self.root.force_focus() is usually considered as evil: it'll give the
> focus to your application whatever the user is doing, which is usually
> *really* annoying. So I guess a lot of window managers just refuse to do
> it; this may be what happens here.
>
> But self.canvas.update() should work. If it doesn't, then there are
> probably limitations on your platform that prevents it to work... Do you
> happen to have other applications that can update their display while they
> don't have the focus? Do they have the same problem? If they do, it's
> probably a limitation on the platform and I guess you won't be able to do
> anything about it... BTW, what platform are you on?
>
> > Thanks!
> > Blaine
>
> HTH
> --
> python -c "print ''.join([chr(154 - ord(c)) for c in
> 'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"

Thanks a lot for the response! To answer your first question - I'm
using pack() there because I didn't mean to slip it in - this is
something I wrote very quickly and was playing around with... and
pack() should not be there, heh.

I'm just using OS X with Python 2.5.  For the record - I don't mind if
the application forces focus.  The only time I will be running this
emulator is when I'm trying to use it.

I'll try the update() again.  I would want to use that on the canvas
itself right? Not the root window?

Blaine



More information about the Python-list mailing list