tkinter checkbutton different image

后端 未结 1 890
孤城傲影
孤城傲影 2021-01-25 07:43

I want a tkinter checkbutton that basically has my own images for on and off, so not the default checkbutton. I\'ve searched the internet for a solution, but couldn\'t find anyt

1条回答
  •  有刺的猬
    2021-01-25 08:45

    You need to set the image option for the unselected state, and the selectimage option for the selected state. You also need to set indicatoron to False so that tkinter doesn't show the default indicator.

    Here is a simple example:

    import tkinter as tk
    root = tk.Tk()
    
    on_image = tk.PhotoImage(width=48, height=24)
    off_image = tk.PhotoImage(width=48, height=24)
    on_image.put(("green",), to=(0, 0, 23,23))
    off_image.put(("red",), to=(24, 0, 47, 23))
    
    var1 = tk.IntVar(value=1)
    var2 = tk.IntVar(value=0)
    cb1 = tk.Checkbutton(root, image=off_image, selectimage=on_image, indicatoron=False,
                         onvalue=1, offvalue=0, variable=var1)
    cb2 = tk.Checkbutton(root, image=off_image, selectimage=on_image, indicatoron=False,
                         onvalue=1, offvalue=0, variable=var2)
    
    cb1.pack(padx=20, pady=10)
    cb2.pack(padx=20, pady=10)
    
    root.mainloop()
    

    0 讨论(0)
提交回复
热议问题