Python tkinter checkbutton不同的图像

Python tkinter checkbutton不同的图像,python,tkinter,styles,ttk,Python,Tkinter,Styles,Ttk,我想要一个tkinter checkbutton,它基本上有我自己的图像用于打开和关闭,所以不是默认的checkbutton。我在互联网上搜索了一个解决方案,但什么也找不到。我想它可能与ttk风格,但我不知道如何可能 尝试更改checkbutton中的selectimage选项,但完全无效 编辑: 将指示灯设置为false,然后更改图像并选择image works您需要将图像选项设置为未选定状态,将selectimage选项设置为选定状态。您还需要将indicatoron设置为False,以便t

我想要一个tkinter checkbutton,它基本上有我自己的图像用于打开和关闭,所以不是默认的checkbutton。我在互联网上搜索了一个解决方案,但什么也找不到。我想它可能与ttk风格,但我不知道如何可能

尝试更改checkbutton中的selectimage选项,但完全无效

编辑: 将指示灯设置为false,然后更改图像并选择image works

您需要将图像选项设置为未选定状态,将selectimage选项设置为选定状态。您还需要将indicatoron设置为False,以便tkinter不会显示默认指示器

下面是一个简单的例子:

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()
您需要为未选定状态设置图像选项,为选定状态设置selectimage选项。您还需要将indicatoron设置为False,以便tkinter不会显示默认指示器

下面是一个简单的例子:

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()
不是默认的复选按钮:Try indicatoron=False。您的问题并显示您的尝试。不是默认的复选按钮:Try indicatoron=False。回答你的问题并展示你的想法。