How to disable custom button - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: How to disable custom button (/thread-31322.html) |
How to disable custom button - Sancho_Pansa - Dec-04-2020 Hello, In my project I use custom widgets. When I try to disable a button self.myCustomButton.config(state='disabled')error occurs: Any ideas ?Thanks RE: How to disable custom button - buran - Dec-04-2020 what GUI framework? RE: How to disable custom button - Sancho_Pansa - Dec-04-2020 (Dec-04-2020, 09:15 AM)buran Wrote: what GUI framework?tkinter RE: How to disable custom button - buran - Dec-04-2020 can you post minimal reproducible example and the full traceback you get? RE: How to disable custom button - Sancho_Pansa - Dec-04-2020 (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 = isClickedHere 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:
RE: How to disable custom button - buran - Dec-04-2020 Your ButtonWidget inherits from generic Widget . Widget , unlike Button has no attribute state , so you cannot configure it via Widget.config() methodYou 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 ?
RE: How to disable custom button - Sancho_Pansa - Dec-04-2020 Thanks. I didn't notice this disable() RE: How to disable custom button - buran - Dec-04-2020 (Dec-04-2020, 01:45 PM)Sancho_Pansa Wrote: I didn't notice this disable()I thought you wrote it in the first place :-) |