Python Forum
Errors when trying to disable tkinter checkbutton
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Errors when trying to disable tkinter checkbutton
#4
The method checkboxes.addCheckBox returns an IntVar. It does not return a Checkbutton. You cannot disable an IntVar.

Your method checkboxes.chkbxClick is a mess. You must have a lot of global variables because all of the variables used here are undefined.

My guess is somewhere you do this:
root = Tk()
boxes = checkboxes(root)
chkbxItemCard = boxes.addCheckBox(..., cmd=boxes.chkbxClick)
chkbxItemCard is an IntVar. When you try to disable the IntVar you get an error because IntVar does not disable.

What is the checkboxes class supposed to do? As is, it doesn't appear to do anything other than define two functions. Here's an example that creates a Checkbox class that is a tk.Checkbutton with some extra features.
import tkinter as tk

class Checkbox(tk.Checkbutton):
    """I add an IntVar to a check button"""
    var_types = {bool:tk.BooleanVar, int:tk.IntVar, float:tk.DoubleVar}

    def __init__(self, parent, x, y, onvalue=1, offvalue=0, **kwargs):
        self.variable = self.var_types.get(type(onvalue), tk.StringVar)(value=offvalue)
        super().__init__(parent, variable=self.variable, onvalue=onvalue, offvalue=offvalue, **kwargs)
        self.place(x=x, y=y)

    def value(self):
        return self.variable.get()

    def set_value(self, value):
        self.variable.set(value)

class App(tk.Tk):
    def __init__(self, title=None, geometry="250x120", **kwargs):
        super().__init__(**kwargs)
        if geometry is not None:
            self.geometry(geometry)
        if title is not None:
            self.wm_title(title)

        self.printer = Checkbox(self,10, 10, text="Printer")
        self.printer.config(command=lambda: print(self.printer.value()))

        self.setter = Checkbox(self, 10, 50, text="Setter")
        self.setter.config(command=lambda: self.printer.set_value(self.setter.value()))

        self.enabler = Checkbox(self, 10, 90, text="Disabler", onvalue="disabled", offvalue="normal")
        self.enabler.config(command=lambda: self.printer.config(state=self.enabler.value()))

app = App("Checkboxes")
app.mainloop()
Reply


Messages In This Thread
RE: Errors when trying to disable tkinter checkbutton - by deanhystad - Feb-16-2022, 05:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  an easy way to disable exception handling Skaperen 6 5,552 Jun-02-2019, 10:38 PM
Last Post: Gribouillis
  disable a block of code Skaperen 5 13,538 Aug-20-2018, 07:55 AM
Last Post: Skaperen
  gnureadline: disable temporarily? klaymen 1 2,519 May-08-2018, 11:16 AM
Last Post: Larz60+
  Checkbutton code not working ToddRyler 4 3,230 Dec-24-2017, 12:34 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020