Home > OS >  Checkbutton is not clickable after adding an image to it
Checkbutton is not clickable after adding an image to it

Time:04-06

I started working with tkinter recently and I have the following problem, I need to make the check box bigger but that is only possible with adding an image. The problem is that whenever I add an image to a button it becomes unclickable and the image is not displayed, here is my source code (part of a bigger project). My goal is to display some information and let the user decide which option he gets to keep using the check button. Any help is appreciated.

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

I tried some options that I saw on the forum, in another file they worked fine but when I tried to implement it in the project itself the checkbutton is still unclickable and it doesn't display the images either. tkinter checkbutton different image I tried to replicate Bryan's answer, but no luck there. Also didn't receive any console error message.

CodePudding user response:

As @furas pointed in the comments above, the problem got fixed with keeping the images as member variables of the class, also the button became clickable after removing the self.toggle(var) command from checkbutton

  • Related