ttk 标签不正常
ttk label doesn't behave properly
当我更改图像的前景色时,包含位图图像的 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)
程序运行正常。
任何帮助将不胜感激。
我正在使用 python 3.5 和 windows Vista
尝试将更改后的图像重新分配给标签:
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)
当我更改图像的前景色时,包含位图图像的 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)
程序运行正常。 任何帮助将不胜感激。
我正在使用 python 3.5 和 windows Vista
尝试将更改后的图像重新分配给标签:
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)