Python Forum
[Tkinter] binding versus disable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] binding versus disable
#1
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
Reply
#2
Try lbl.update_idletasks() after configuring the label perhaps.
Reply
#3
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 !

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 ? Smile
Paul
Reply
#4
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.
Gribouillis likes this post
Reply
#5
OK, thanks, unbind it is.

Paul
Reply
#6
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()
Gribouillis likes this post
Reply
#7
OK, thanks, nice touch !
I'll go for label.unbind, because once "disabled", it has no
more use, and i make it disappear.

Paul
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to disable custom button Sancho_Pansa 7 1,000 Dec-04-2020, 02:21 PM
Last Post: buran
  Disable entry field and still see value scratchmyhead 5 1,155 May-11-2020, 08:09 PM
Last Post: menator01
  [Tkinter] Binding Entry box to <Button-3> created in for loop iconit 5 1,868 Apr-22-2020, 05:47 AM
Last Post: iconit
  TkInter Binding Buttons ifigazsi 5 1,094 Apr-06-2020, 08:30 AM
Last Post: ifigazsi
  Making text clickable with binding DT2000 10 1,987 Apr-02-2020, 10:11 PM
Last Post: DT2000
  [Tkinter] Setting Binding to Entry created with a loop? p_hobbs 1 964 Nov-25-2019, 10:29 AM
Last Post: Larz60+
  Binding functions in Qt Designerr Mocap 12 2,605 Aug-22-2019, 03:38 PM
Last Post: Denni
  Binding functions to Menus in tkinter?? Mocap 1 1,156 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,122 Aug-14-2018, 12:37 PM
Last Post: buran

Forum Jump:

User Panel Messages

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