ttk标签行为不正常

2024-04-26 23:37:35 发布

您现在位置:Python中文网/ 问答频道 /正文

当我更改图像的前景颜色时,包含位图图像的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()

图像的前景色应该每秒钟都改变一次,除非你用鼠标飞过图像。 只需更换线路:

^{pr2}$

有:

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

这个程序很有效。 任何帮助都将不胜感激。在

我在WindowsVista中使用Python3.5


Tags: 图像importimgtkinterasroot标签change