Python Forum
How to disable custom button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to disable custom button
#1
Hello,
In my project I use custom widgets.
When I try to disable a button
self.myCustomButton.config(state='disabled')
error occurs:
Error:
unknown option "-state"
Any ideas ?
Thanks
Reply
#2
what GUI framework?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Dec-04-2020, 09:15 AM)buran Wrote: what GUI framework?
tkinter
Reply
#4
can you post minimal reproducible example and the full traceback you get?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Dec-04-2020, 09:33 AM)buran Wrote: can you post minimal reproducible example and the full traceback you get?
Here is custom Button:
from tkinter import *
from tkinter.font import Font
import Utils.Constant as Constant
from time import *
from View.Widget.Widget import Widget

class ButtonWidget(Widget):
    """description of class"""

    def __init__(self, parent, buttonText, buttonWidth, buttonHeight):
        Widget.__init__(self, parent)
    
        self.text = StringVar()
        self.text.set(buttonText)
        self.enabled = True
        self.clicked = False

        self.buttonFont = Font(family=Constant.FontFamily, size=Constant.FontSmallSize)
        self.pixelVirtual = PhotoImage(width=1, height=1)

        self.border = Frame(self, highlightbackground=Constant.LightBlue, highlightcolor=Constant.LightBlue, highlightthickness=1)
        self.button = Button(self.border, relief=FLAT, textvariable=self.text, width=buttonWidth, height=buttonHeight, cursor="hand2",
                        background=Constant.White, foreground=Constant.LightBlue, font=self.buttonFont, image=self.pixelVirtual, compound="c")
        
        self.button.bind("<ButtonPress>", self.onPress)
        self.button.bind("<ButtonRelease>", self.onRelease)

        self.button.pack(expand=True, fill='both')
        self.border.pack()

    def getTkWidget(self):
        return self.button

    def setText(self, buttonText):
        self.text.set(buttonText)

    def onPress(self, event):
        if self.enabled:
            self.border.config(highlightbackground=Constant.LightBlue)

    def onRelease(self, event):
        if self.enabled:
            self.border.config(highlightbackground=Constant.LightBlue)
            self.button.config(foreground=Constant.LightBlue)

    def enable(self):
        self.enabled = True
        self.button.configure(state=NORMAL)
        self.border.configure(highlightbackground=Constant.LightBlue, highlightcolor=Constant.LightBlue)

    def disable(self):
        self.enabled = False
        self.button.configure(state=DISABLED)
        self.border.configure(highlightbackground=Constant.Disabled, highlightcolor=Constant.Disabled)

    def setClicked(self, isClicked):
        self.clicked = isClicked
Here is how I define button and try to disable it:
        MyButton = ButtonWidget(self, "Apply", buttonWidth=40, buttonHeight=15)
        MyButton.config(state='disabled')
And here is output:
Error:
Traceback (most recent call last): File "D:\DATA_PYE\python_source_SpectraMon\Main.py", line 20, in <module> mainWindow = MainWindowView(root) File "D:\DATA_PYE\python_source_SpectraMon\View\MainWindowView.py", line 44, in __init__ self.createView(parent) File "D:\DATA_PYE\python_source_SpectraMon\View\MainWindowView.py", line 66, in createView self.createPages() File "D:\DATA_PYE\python_source_SpectraMon\View\MainWindowView.py", line 152, in createPages self.monitorPage = MonitorView(self.contentFrame) File "D:\DATA_PYE\python_source_SpectraMon\View\MonitorView.py", line 43, in __init__ self.createView() File "D:\DATA_PYE\python_source_SpectraMon\View\MonitorView.py", line 110, in createView self.createMonitorFrame() File "D:\DATA_PYE\python_source_SpectraMon\View\MonitorView.py", line 130, in createMonitorFrame self.createLoopsStatesFrame() File "D:\DATA_PYE\python_source_SpectraMon\View\MonitorView.py", line 382, in createLoopsStatesFrame MyButton.config(state='disabled') File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1639, in configure return self._configure('configure', cnf, kw) File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1629, in _configure self.tk.call(_flatten((self._w, cmd)) + self._options(cnf)) _tkinter.TclError: unknown option "-state"
Reply
#6
Your ButtonWidget inherits from generic Widget. Widget, unlike Button has no attribute state, so you cannot configure it via Widget.config() method

You have defined ButtonWidget.disable() method. Why not call it directly - MyButton.disable() instead of using MyButton.config()?
Or why not inherit from Button instead of Widget?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Thanks. I didn't notice this disable()
Reply
#8
(Dec-04-2020, 01:45 PM)Sancho_Pansa Wrote: I didn't notice this disable()
I thought you wrote it in the first place :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] binding versus disable DPaul 6 6,599 May-05-2021, 05:17 PM
Last Post: DPaul
  How to disable focus on Frame in Tkinter? szafranji 1 2,971 May-13-2020, 10:45 PM
Last Post: DT2000
  Disable entry field and still see value scratchmyhead 5 4,970 May-11-2020, 08:09 PM
Last Post: menator01
  [Tkinter] Tkinter custom widget styling and creating custom theme karolp 6 4,747 May-06-2020, 06:11 PM
Last Post: karolp
  [Tkinter] how can disable menu [About] when Toplevel is active balenaucigasa 0 2,642 Oct-25-2019, 09:49 PM
Last Post: balenaucigasa
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,949 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] disable/enable button | stopwatch proj robertofreemano 1 6,268 Jul-18-2018, 09:52 PM
Last Post: Larz60+
  Disable Enter Key PyQt5 Wizard maffaz 1 6,658 Jul-02-2018, 09:45 AM
Last Post: maffaz
  [Tkinter] Disable anti aliasing for text AnonymousNobody 3 6,784 Aug-11-2017, 07:54 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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