[Tkinter] binding versus disable - 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: [Tkinter] binding versus disable (/thread-33556.html) |
binding versus disable - DPaul - May-05-2021 Hi, I have a tkinter label, that i bind to some function: lbl.bind('<Button-1>',some_function) Works just fine! After some events, i want to disable this click possibility on the label: lbl.config(state = DISABLED) This shows some change in the appearance of the label, but it does not stop the clicking. i found a solution by unbinding the label: lbl.unbind('<Button-1>') My question: i did not read anywhere that binding takes priority on 'DISABLED'. Is this actually a matter of hierarchy? Thanks, Paul RE: binding versus disable - Gribouillis - May-05-2021 Try lbl.update_idletasks() after configuring the label perhaps.
RE: binding versus disable - DPaul - May-05-2021 (May-05-2021, 08:18 AM)Gribouillis Wrote: Try OK, that works ! (I thought it was not necessary because the app is slow_moving, waiting for user input all the time.) Thanks ! This method has the added benefit of exposing a formatting mistake (columnspan) in my tkinter "grid", which did never show using "unbind" . Strange ! Paul Edit: After examination : it does not work ! It does expose the grid error, but is does not prevent the clicking. So back to unbind ? Paul RE: binding versus disable - deanhystad - May-05-2021 If you look at the Tk documentation you would see that setting state for a label only affects how the label appears. https://www.tcl.tk/man/tcl8.6/TkCmd/label.htm#M7 The same document says this about buttons: Quote:Disabled state means that the button should be insensitive: the default bindings will refuse to activate the widget and will ignore mouse button presses.This does not "disable" bindings set using the bind command. For example, this gives you an inactive button that responds to pressing the left mouse button. button = tk.Button(parent, state=tk.DISABLED, command=press) button.bind("<Button-1>", press)tkinter is such a thin wrapper around Tk that I don't waste my time with tkinter docs anymore. If you want to know how things work, read the Tk documentation. RE: binding versus disable - DPaul - May-05-2021 OK, thanks, unbind it is. Paul RE: binding versus disable - Yoriz - May-05-2021 If you want to go with using the disabled state you could check the state in the event handler and just return if it's disabled. import tkinter as tk class App(tk.Tk): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.label = tk.Label(self, text='Click me') self.label.bind('<Button-1>', self.on_label_click) self.label.pack() self.button = tk.Button(self, text='toggle label state') self.button.bind('<Button-1>', self.on_button_click) self.button.pack() def on_label_click(self, event): if 'disabled' in event.widget.config('state'): print('Label is disabled: Do nothing') return print('Label is enabled: Do something') def on_button_click(self, event): if 'disabled' in self.label.config('state'): self.label.config(state='normal') else: self.label.config(state='disabled') if __name__ == "__main__": app = App() app.mainloop() RE: binding versus disable - DPaul - May-05-2021 OK, thanks, nice touch ! I'll go for label.unbind, because once "disabled", it has no more use, and i make it disappear. Paul |