Checkbutton 添加图片后无法点击

Checkbutton is not clickable after adding an image to it

我最近开始使用 tkinter,我遇到了以下问题,我需要使复选框更大,但这只能通过添加图像来实现。问题是,每当我将图像添加到按钮时,它就会变得不可点击并且图像不会显示,这是我的源代码(更大项目的一部分)。我的目标是显示一些信息并让用户决定他可以继续使用检查按钮的哪个选项。感谢任何帮助。

import tkinter as tk
import tkcalendar as tkc
LARGE_FONT = ("HELVETICA", 32, 'bold')
NORMAL_FONT = ("calibri", 18)
class ConstituireDosar(tk.Toplevel):
    def __init__(self, controller):
        tk.Toplevel.__init__(self)
        self.update_idletasks()
        # self.dosar = dosar
        self.controller = controller

        self.minsize(651, 569)
        # self.maxsize(651, 569)

        frame_titlu = tk.Frame(self)
        frame_titlu.grid(row = 0, column = 0)
        frame_continut = tk.Frame(self)
        frame_continut.grid(row = 1, column = 0, sticky = "w")
        frame_acte = tk.Frame(self)
        frame_acte.grid(row = 2, column = 0)
      
        titlu = tk.Label(frame_titlu, font = LARGE_FONT, text = "Constituire Dosar")
        titlu.grid(row = 0 , column = 0, padx = 10, pady = 15)
        data_emiterii = tk.Label(frame_continut, font = NORMAL_FONT,text = "Data emiterii documentului:")
        data_emiterii.grid(row = 1, column = 0, padx = 10, pady = 5, sticky = "w")
        self.cal = tkc.DateEntry(frame_continut, date_pattern = "DD/MM/YYYY", width = 20)
        self.cal.grid(row = 2, column = 0, padx = 10, pady = 5, sticky = "w")
        debitori_label = tk.Label(frame_continut, font = NORMAL_FONT, text = "Selecteaza debitorii.")
        debitori_label.grid(row = 3, column = 0, padx = 10, pady = 5, sticky = "w")
        debitori = []
        tip_debitori = []
       
        for i in range(2):
            debitori.append("Person %s " % str(i))
            tip_debitori.append("Person %s type" % str(i))

        for i in range(len(debitori)):
            print(debitori[i])            
       
        row_i = 4
        self.vars_debitori = []
       
       
        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))


        for i in range(len(debitori)):
            var = tk.IntVar(frame_continut, value = 0)
            interior = debitori[i] + " - " + tip_debitori[i]
            # Checkbutton(ws, image=switch_off, selectimage=switch_on, onvalue=1, offvalue=0, variable=cb1, indicatoron=False, command=switchState)
            checkbuton = tk.Checkbutton (frame_continut, bd = 5, image = off_image, selectimage = on_image, indicatoron = False, onvalue = 1, offvalue = 0, variable = var, state = tk.ACTIVE, command = lambda: self.toggle(var))
            checkbuton.grid(row = row_i, column = 0, padx = 20, pady = 5, sticky = "nw")
            checkbuton.image = off_image
            # checkbuton.select()
            self.vars_debitori.append(var)
            row_i += 1

        self.vars_acte = []

        acte = ["Acte de Procedura", "Incheiere de Admitere", "Cerere de Incuviintare", "Instiintare Creditor"]
        for i in range(4):
            v = tk.IntVar()
            check = tk.Checkbutton(frame_acte, font = NORMAL_FONT, text = acte[i], variable = v)
            check.grid(row = row_i, column = 0, padx = 10, pady = 5)
            check.select()
            self.vars_acte.append(v)
            row_i += 1
     
        emite_acte = tk.Button(frame_acte, font = NORMAL_FONT, text = "Emite acte.", command = self.emite_acte)
        emite_acte.grid(row = row_i, column = 1, padx = 15, pady = 30, ipadx = 70, ipady = 10)
        emite_acte.configure(bg = '#218838', fg = '#FFFFFF')

        buton_cancel = tk.Button(frame_acte, font = NORMAL_FONT, text = "Cancel", command = lambda: self.destroy())
        buton_cancel.grid(row = row_i, column = 0, padx = 15, pady = 30, ipadx = 70, ipady = 10)
        buton_cancel.configure(bg = "red", fg = '#FFFFFF')

    def emite_acte(self):
        print(self.cal.get_date().strftime("%d/%m/%y"))
        print(self.winfo_height(), self.winfo_width())
       

if __name__ == "__main__":
        root = tk.Tk() 
        app = ConstituireDosar(root)
        app.protocol("WM_DELETE_WINDOW", root.destroy)
        root.withdraw()
        root.mainloop()

我尝试了一些我在论坛上看到的选项,在另一个文件中它们工作正常,但是当我尝试在项目本身中实现它时,复选按钮仍然无法点击并且它也不显示图像。 我试图复制 Bryan 的回答,但没有成功。也没有收到任何控制台错误消息。

正如@furas 在上面的评论中指出的那样,通过将图像保留为 class 的成员变量来解决问题,并且在删除 self.toggle(var) 命令后按钮变得可点击来自复选按钮