tkinter checkbutton 不同的图像 [英] tkinter checkbutton different image
问题描述
我想要一个 tkinter checkbutton,它基本上有我自己的打开和关闭图像,所以不是默认的 checkbutton.我已经在互联网上搜索了解决方案,但找不到任何东西.我认为 ttk 样式可能可行,但我不确定如何
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 anything. I'm thinking it might be possible with ttk styling, but i'm not sure how
尝试更改复选按钮中的 selectimage 选项,但没有任何作用
tried changing the selectimage option in the checkbutton, which did absolutely nothing
将indicatoron设置为false,然后更改图像和选择图像工作
edit: turning indicatoron to false, then changing image and selectimage works
推荐答案
未选中状态需要设置image
选项,选中状态需要设置selectimage
选项状态.您还需要将 indicatoron
设置为 False
以便 tkinter 不显示默认指标.
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.
这是一个简单的例子:
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()
这篇关于tkinter checkbutton 不同的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!