gpt4 book ai didi

python - ttk 标签不正常

转载 作者:行者123 更新时间:2023-12-01 23:30:08 30 4
gpt4 key购买 nike

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

最佳答案

尝试将更改后的图像重新分配给标签:

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)

关于python - ttk 标签不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39468535/

30 4 0