ttk标签表现不正常

时间:2016-09-13 11:08:12

标签: python python-3.x tkinter ttk

当我更改图像的前景色时,包含位图图像的ttk标签不能正常运行。只有ttk标签有这个问题。 Tkinter标签正常工作。

以下是代码:

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()

图像的前景色应该每秒都会改变,除非你用鼠标飞过图像,否则它不会改变。 只需更换一行:

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

使用:

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

并且该程序有效。 任何帮助将不胜感激。

我在Windows Vista中使用python 3.5

1 个答案:

答案 0 :(得分:3)

尝试将更改后的图片重新指定为标签:

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)