python - ttk 标签不正常

标签 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

最佳答案

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

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/

相关文章:

python - 引用一个显示 'local variable ' Final_ans' 在声明之前引用的变量'

Python tkinter 输入提示

Python Tkinter 循环

python - 当 help() 列出方法签名时,斜杠意味着什么?

python - python 2.7/3 中是否需要 unicode(codecs.BOM_UTF8, "utf8")?

python - 将类型(字符串)转换为 pandas.core.series.Series

python - 将较低的值分配给字典中的键

Python Tkinter 字体选择器

python - Django Oracle App 提供 ORA-03113 和 ORA-03135

python - 如何正确地将 PyTZ 添加到 Google App Engine 应用程序?