Search code examples
pythontkinterlabelttk

Multiple images in a ttk label widget


With ttk labels, it is possible to specify multiple images which are displayed according to the label's state. But I can't make it work. Here is the code.

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()

The label is 'active' when the mouse pass over it. So the displayed digit should switch from a 0 to a 1 when the mouse passe over it. But it doesn't work. Any help ? Python 3.5.1 / Windows Vista


Solution

  • I find the docs a bit confusing but it looks like you want 'hover' instead of 'active'.

    I am not aware of any source explaining which state flags are automatically set in which wigdets in which conditions. What I did here was to place the mouse cursor over the label and then query the state by calling label.state().