Python Forum
[Tkinter] binding versus disable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] binding versus disable

I have a tkinter label, that i bind to 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:

My question:
i did not read anywhere that binding takes priority on 'DISABLED'.
Is this actually a matter of hierarchy?

Try lbl.update_idletasks() after configuring the label perhaps.
Thumbs Up 
(May-05-2021, 08:18 AM)Gribouillis Wrote: Try lbl.update_idletasks() after configuring the label perhaps.

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 !



After examination : it does not work !
It does expose the grid error, but is does not prevent the clicking.
So back to unbind ? Smile
If you look at the Tk documentation you would see that setting state for a label only affects how the label appears.

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.
Gribouillis likes this post
OK, thanks, unbind it is.

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.button = tk.Button(self, text='toggle label state')
        self.button.bind('<Button-1>', self.on_button_click)

    def on_label_click(self, event):
        if 'disabled' in event.widget.config('state'):
            print('Label is disabled: Do nothing')

        print('Label is enabled: Do something')

    def on_button_click(self, event):
        if 'disabled' in self.label.config('state'):

if __name__ == "__main__":
    app = App()
Gribouillis likes this post
OK, thanks, nice touch !
I'll go for label.unbind, because once "disabled", it has no
more use, and i make it disappear.


Possibly Related Threads…
Thread Author Replies Views Last Post
  kivy binding issue hammer 8 368 Nov-07-2021, 11:34 PM
Last Post: hammer
  How to disable custom button Sancho_Pansa 7 1,180 Dec-04-2020, 02:21 PM
Last Post: buran
  Disable entry field and still see value scratchmyhead 5 1,380 May-11-2020, 08:09 PM
Last Post: menator01
  [Tkinter] Binding Entry box to <Button-3> created in for loop iconit 5 2,116 Apr-22-2020, 05:47 AM
Last Post: iconit
  TkInter Binding Buttons ifigazsi 5 1,257 Apr-06-2020, 08:30 AM
Last Post: ifigazsi
  Making text clickable with binding DT2000 10 2,255 Apr-02-2020, 10:11 PM
Last Post: DT2000
  [Tkinter] Setting Binding to Entry created with a loop? p_hobbs 1 1,058 Nov-25-2019, 10:29 AM
Last Post: Larz60+
  Binding functions in Qt Designerr Mocap 12 2,859 Aug-22-2019, 03:38 PM
Last Post: Denni
  Binding functions to Menus in tkinter?? Mocap 1 1,278 Jul-23-2019, 01:37 AM
Last Post: Larz60+
  [Kivy] Kivy property (in .kv) loses binding to a variable (in .py) j.crater 3 3,331 Aug-14-2018, 12:37 PM
Last Post: buran

Forum Jump:

User Panel Messages

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