Discussion:
[Tkinter-discuss] Lazy updating of image data in ttk widgets
Michael Lange
2013-03-08 16:52:26 UTC
Permalink
Hi,

I noticed that image data in ttk widgets seem to be updated only when the
widget receives keyboard focus or the mouse pointer enters the widget.
A simple example of what I mean:

#####################################################################
import Tkinter
import ttk

icon1 = ('R0lGODlhFQANAMIAAJaWlu/v7////wAAAP///////////////yH5BAEAAA'
'QALAAAAAAVAA0AAANACLqsQTDCRsOTUYnNucUZIAwDRwofSGhk2aIXq'
'I1tCauruL1bKus1mw/DOr2GkqKphAx1ns0JwEKtRlfWbAyTAAA7')
icon2 = ('R0lGODlhFQANAKEAAJaWlu/v7////////yH5BAEAAAMALAAAAAAVAA0AAA'
'IyhI8Ww80Jw3LNiIuxpBXkv3GD9WkTR5ZCiHrqelJpycquWjszGOs33'
'etIhsRcpYgMMgoAOw==')

root = Tkinter.Tk()
im = Tkinter.PhotoImage(data=icon1)

b = ttk.Button(image=im, text='ttk.Button', compound='left')
b.pack(padx=100, pady=30)
b2 = Tkinter.Button(image=im, text='Tkinter.Button', compound='left')
b2.pack(padx=100, pady=30)

root.bind('<F1>', lambda event: im.configure(data=icon2))
root.mainloop()
#####################################################################

When I start this and press F1, the icon in the Tkinter button is updated
immediately (as expected), the image in the ttk button only when I <Tab>
into the button or put the mouse pointer into the widget.
The system here is debian squeeze, Tk-8.5.8 .

Can anyone confirm this behavior, and is there any known workaround?

Regards

Michael


.-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-.

After a time, you may find that "having" is not so pleasing a thing,
after all, as "wanting." It is not logical, but it is often true.
-- Spock, "Amok Time", stardate 3372.7
Bob Greschke
2013-03-08 17:24:33 UTC
Permalink
Happens on

Python 2.7.2 (default, Jul 18 2011, 14:33:20)
[GCC 4.4.4 20100726 (Red Hat 4.4.4-13)] on linux2
TclVersion 8.5 TkVersion 8.5
with the program running on the above and the output being X'ed to an OSX Mountain Lion iMac.

I just have to mouse over the ttk button and it changes (even if, for example, Mail.app has the display focus). When I mouse over the buttons they get slightly lighter color while the cursor is on them (the 'activebackground' color?)

Same with

Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Post by Michael Lange
Post by Michael Lange
import Tkinter
Tkinter.TclVersion
8.5
Post by Michael Lange
Post by Michael Lange
Tkinter.TkVersion
8.5

on my iMac. Just running the mouse cursor over the button makes it change. No activebackground color change on Mac.

Bob
Post by Michael Lange
Hi,
I noticed that image data in ttk widgets seem to be updated only when the
widget receives keyboard focus or the mouse pointer enters the widget.
#####################################################################
import Tkinter
import ttk
icon1 = ('R0lGODlhFQANAMIAAJaWlu/v7////wAAAP///////////////yH5BAEAAA'
'QALAAAAAAVAA0AAANACLqsQTDCRsOTUYnNucUZIAwDRwofSGhk2aIXq'
'I1tCauruL1bKus1mw/DOr2GkqKphAx1ns0JwEKtRlfWbAyTAAA7')
icon2 = ('R0lGODlhFQANAKEAAJaWlu/v7////////yH5BAEAAAMALAAAAAAVAA0AAA'
'IyhI8Ww80Jw3LNiIuxpBXkv3GD9WkTR5ZCiHrqelJpycquWjszGOs33'
'etIhsRcpYgMMgoAOw==')
root = Tkinter.Tk()
im = Tkinter.PhotoImage(data=icon1)
b = ttk.Button(image=im, text='ttk.Button', compound='left')
b.pack(padx=100, pady=30)
b2 = Tkinter.Button(image=im, text='Tkinter.Button', compound='left')
b2.pack(padx=100, pady=30)
root.bind('<F1>', lambda event: im.configure(data=icon2))
root.mainloop()
#####################################################################
When I start this and press F1, the icon in the Tkinter button is updated
immediately (as expected), the image in the ttk button only when I <Tab>
into the button or put the mouse pointer into the widget.
The system here is debian squeeze, Tk-8.5.8 .
Can anyone confirm this behavior, and is there any known workaround?
Regards
Michael
.-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-.
After a time, you may find that "having" is not so pleasing a thing,
after all, as "wanting." It is not logical, but it is often true.
-- Spock, "Amok Time", stardate 3372.7
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss at python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
Michael Lange
2013-03-08 18:02:20 UTC
Permalink
On Fri, 8 Mar 2013 10:24:33 -0700
Post by Bob Greschke
Happens on
Python 2.7.2 (default, Jul 18 2011, 14:33:20)
[GCC 4.4.4 20100726 (Red Hat 4.4.4-13)] on linux2
TclVersion 8.5 TkVersion 8.5
with the program running on the above and the output being X'ed to an
OSX Mountain Lion iMac.
(...)
Post by Bob Greschke
Same with
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Post by Michael Lange
import Tkinter
Tkinter.TclVersion
8.5
Post by Michael Lange
Tkinter.TkVersion
8.5
Ok, then it is at least not a bug specific for my version of Tk.

Experimenting a little I just found that it is possible to update the
image programatically if I force tk to redraw the button e.g. with this
modification of my example:

def test(event=None):
im.configure(data=icon2)
b.config(state=b['state'])

root.bind('<F1>', test)

It also works with b.config(image=b['image']) or
b.config(text=b['text']). Hmmm...

Another try, I changed my example callback into:

s = ttk.Style()
def test(event=None):
im.configure(data=icon2)
s.theme_use(s.theme_use())

root.bind('<F1>', test)

Here this works too, and this one might actually be usable!
Can you confirm that this does the trick?

Regards

Michael


.-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-.

Without facts, the decision cannot be made logically. You must rely on
your human intuition.
-- Spock, "Assignment: Earth", stardate unknown
Bob Greschke
2013-03-08 18:12:50 UTC
Permalink
Post by Michael Lange
On Fri, 8 Mar 2013 10:24:33 -0700
Post by Bob Greschke
Happens on
Python 2.7.2 (default, Jul 18 2011, 14:33:20)
[GCC 4.4.4 20100726 (Red Hat 4.4.4-13)] on linux2
TclVersion 8.5 TkVersion 8.5
with the program running on the above and the output being X'ed to an
OSX Mountain Lion iMac.
(...)
Post by Bob Greschke
Same with
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Post by Michael Lange
import Tkinter
Tkinter.TclVersion
8.5
Post by Michael Lange
Tkinter.TkVersion
8.5
Ok, then it is at least not a bug specific for my version of Tk.
Experimenting a little I just found that it is possible to update the
image programatically if I force tk to redraw the button e.g. with this
im.configure(data=icon2)
b.config(state=b['state'])
root.bind('<F1>', test)
It also works with b.config(image=b['image']) or
b.config(text=b['text']). Hmmm...
s = ttk.Style()
im.configure(data=icon2)
s.theme_use(s.theme_use())
root.bind('<F1>', test)
Here this works too, and this one might actually be usable!
Can you confirm that this does the trick?
Yup. The "Another try" works here on both systems.

Bob
Michael Lange
2013-03-08 19:31:48 UTC
Permalink
On Fri, 8 Mar 2013 11:12:50 -0700
Post by Bob Greschke
Yup. The "Another try" works here on both systems.
Cool, then there seems to be at last a usable way to bring my playing
around with switchable icon themes to a successful end.

Regards

Michael



.-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-.

I realize that command does have its fascination, even under
circumstances such as these, but I neither enjoy the idea of command
nor am I frightened of it. It simply exists, and I will do whatever
logically needs to be done.
-- Spock, "The Galileo Seven", stardate 2812.7
Emiliano Gavilan
2013-03-08 19:50:33 UTC
Permalink
Post by Michael Lange
Hi,
I noticed that image data in ttk widgets seem to be updated only when the
widget receives keyboard focus or the mouse pointer enters the widget.
This is Tk bug #3462273
(http://sourceforge.net/tracker/?func=detail&atid=112997&aid=3462273&group_id=12997)

The workaround is to force the widget redraw. For example, sending an
<Expose> event.

Regards
Emiliano

Loading...