Search code examples
pythonpython-3.xtkinterttk

ttk label doesn't behave properly


A ttk Label which contains a bitmap image doesn't behave properly when I change the image's foreground color. Only ttk Labels have this problem. Tkinter labels works properly.

Here is the code:

import tkinter as tk
import tkinter.ttk as ttk

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
};
"""

root = tk.Tk()

img = tk.BitmapImage(data=BITMAP0, foreground='Lime', background='Black')

label = ttk.Label(root, image=img)
label.pack()

color = ['red', 'yellow', 'lime', 'white']

def change_color(n):
    img.config(foreground=color[n])
    if n == 3:
        root.after(1000, change_color, 0)
    else:
        root.after(1000, change_color, n+1)

root.after(1000, change_color, 0)

root.mainloop()

The image's foreground color should change every second, it doesn't, unless you fly over the image with the mouse. Just replace the line:

label = ttk.Label(root, image=img)

with:

label = tk.Label(root, image=img)

and the program works. Any help would be appreciated.

I am using python 3.5 with windows Vista


Solution

  • Try to reassign the changed image to label:

    def change_color(n):
        img.config(foreground=color[n%4])
        label.config(image=img) # reassign the changed image to label
        root.after(1000, change_color, n+1)