Label behavior's difference between tkinter and ttk

Terry Reedy tjreedy at udel.edu
Tue Apr 5 17:27:34 EDT 2016


On 4/5/2016 2:57 AM, ast wrote:
> Hello
>
> I currently migrate a GUI from tkinter to ttk and I found a problem
>
> Here is a piece of code, with comments which explain what is wrong.

Here is a complete program that eliminates manual entry and uses 
.mainloop and .after for the delays.

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()

BITMAP0 = """
#define zero_width 24
#define zero_height 32
static char zero_bits[] = {
0x00,0x00,0x00, 0x00,0x00,0x00, 0xf0,0x3c,0x0f, 0xf0,0x3c,0x0f,
0xf0,0x3c,0x0f, 0xf0,0x3c,0x0f, 0x00,0x00,0x00, 0x00,0x00,0x00,
0xf0,0x00,0x0f, 0xf0,0x00,0x0f, 0xf0,0x00,0x0f, 0xf0,0x00,0x0f,
0x00,0x00,0x00, 0x00,0x00,0x00, 0xf0,0x00,0x0f, 0xf0,0x00,0x0f,
0xf0,0x00,0x0f, 0xf0,0x00,0x0f, 0x00,0x00,0x00, 0x00,0x00,0x00,
0xf0,0x00,0x0f, 0xf0,0x00,0x0f, 0xf0,0x00,0x0f, 0xf0,0x00,0x0f,
0x00,0x00,0x00, 0x00,0x00,0x00, 0xf0,0x3c,0x0f, 0xf0,0x3c,0x0f,
0xf0,0x3c,0x0f, 0xf0,0x3c,0x0f, 0x00,0x00,0x00, 0x00,0x00,0x00
};
"""

img = tk.BitmapImage(data=BITMAP0, foreground='white', background='black')
label_tk = tk.Label(root, image=img)
label_ttk = ttk.Label(root, image=img)
label_tk.pack()
label_ttk.pack()
colors = ('red', 'lime', 'yellow')
def newcolor(n):
     img.config(foreground=colors[n])
     n += 1
     if n < len(colors):
         root.after(1000, newcolor, n)
root.after(1000, newcolor, 0)
root.mainloop()

> # The graphic is not refreshed after entering following commands
> # when Label comes from ttk. You have to fly over the label with # the
> mouse to get the right color.

The reconfiguration is now automatic, but the result is the same.  The 
tk label image changes, the ttk label image does not without mousing 
over the label (making it 'active')

> It looks like a ttk bug, isn't it ?

Perhaps.  If we do not get an answer, I might post a Stackoverflow 
question.  Then the tracker.

The difference between tk and ttk Label image options is that the latter 
can have multiple images for different states.  But the single option 
should be used in all states.

A general difference for ttk is the state vector.  Mouse over changes 
the state to active, so for Label, ttk looks to see if there is an image 
for the active state

> I am using python 3.5.1

Ditto, on Windows.

> I tried a root.update_idletasks() to refresh the graphic
> but it changed nothings.

Ditto for root.update().  Both are incorporated in .mainloop().

-- 
Terry Jan Reedy




More information about the Python-list mailing list