ttk标签小部件中的多个图像

时间:2016-10-03 08:19:56

标签: python tkinter label ttk

使用ttk标签,可以指定根据标签状态显示的多个图像。但我不能让它发挥作用。这是代码。

from tkinter import *
from tkinter.ttk import *

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
};
"""

BITMAP1 = """
#define one_width 24
#define one_height 32
static char one_bits[] = {
0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x0f, 0x00,0x00,0x0f,
0x00,0x00,0x0f, 0x00,0x00,0x0f, 0x00,0x00,0x00, 0x00,0x00,0x00,
0x00,0x00,0x0f, 0x00,0x00,0x0f, 0x00,0x00,0x0f, 0x00,0x00,0x0f,
0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x0f, 0x00,0x00,0x0f,
0x00,0x00,0x0f, 0x00,0x00,0x0f, 0x00,0x00,0x00, 0x00,0x00,0x00,
0x00,0x00,0x0f, 0x00,0x00,0x0f, 0x00,0x00,0x0f, 0x00,0x00,0x0f,
0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x0f, 0x00,0x00,0x0f,
0x00,0x00,0x0f, 0x00,0x00,0x0f, 0x00,0x00,0x00, 0x00,0x00,0x00
};
"""

root = Tk()
img0 = BitmapImage(data=BITMAP0, foreground='lime', background='black')
img1 = BitmapImage(data=BITMAP1, foreground='lime', background='black')
label = Label(root, image=(img0, 'active', img1))
label.pack()

当鼠标经过标签时,标签为“有效”。因此,当鼠标移过它时,显示的数字应该从0切换到1。但它不起作用。 有帮助吗? Python 3.5.1 / Windows Vista

2 个答案:

答案 0 :(得分:1)

我发现文档有点令人困惑,但看起来你想要'hover'而不是'active'

我不知道有任何消息来源解释哪些状态标志会自动设置在哪些条件的wigdets中。我在这里做的是将鼠标光标放在标签上,然后通过调用label.state()来查询状态。

答案 1 :(得分:0)

ttk.Label似乎不接受状态。但是,输入/退出绑定确实可以与样式配置设置一起使用。输出为绿色的0/1。

import tkinter as tk
import tkinter.ttk as ttk

def Limg1(*args):
  s.configure('zL1.TLabel', image=img1)

def Limg0(*args):
  s.configure('zL1.TLabel', image=img0)

root = tk.Tk()
s = ttk.Style()
s.theme_use("classic") #3D/Color-alt/classic/default, Color-clam). 

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
};
"""

BITMAP1 = """
#define one_width 24
#define one_height 32
static char one_bits[] = {
0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x0f, 0x00,0x00,0x0f,
0x00,0x00,0x0f, 0x00,0x00,0x0f, 0x00,0x00,0x00, 0x00,0x00,0x00,
0x00,0x00,0x0f, 0x00,0x00,0x0f, 0x00,0x00,0x0f, 0x00,0x00,0x0f,
0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x0f, 0x00,0x00,0x0f,
0x00,0x00,0x0f, 0x00,0x00,0x0f, 0x00,0x00,0x00, 0x00,0x00,0x00,
0x00,0x00,0x0f, 0x00,0x00,0x0f, 0x00,0x00,0x0f, 0x00,0x00,0x0f,
0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x0f, 0x00,0x00,0x0f,
0x00,0x00,0x0f, 0x00,0x00,0x0f, 0x00,0x00,0x00, 0x00,0x00,0x00
};
"""

img0 = tk.BitmapImage(data=BITMAP0, foreground='lime', background='black')
img1 = tk.BitmapImage(data=BITMAP1, foreground='lime', background='black')

s.configure('zL1.TLabel', image=img0)

label = ttk.Label(root, style='zL1.TLabel')
label.grid(column=0,row=0,sticky='nsew');

label.bind('<Enter>',Limg1);
label.bind('<Leave>',Limg0);
root.mainloop()