Python Forum
[Tkinter] binding versus disable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] binding versus disable
#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


Messages In This Thread
binding versus disable - by DPaul - May-05-2021, 07:38 AM
RE: binding versus disable - by Gribouillis - May-05-2021, 08:18 AM
RE: binding versus disable - by DPaul - May-05-2021, 08:47 AM
RE: binding versus disable - by deanhystad - May-05-2021, 11:23 AM
RE: binding versus disable - by DPaul - May-05-2021, 12:10 PM
RE: binding versus disable - by Yoriz - May-05-2021, 04:22 PM
RE: binding versus disable - by DPaul - May-05-2021, 05:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Key Binding scope angus1964 1 1,243 Jun-30-2022, 08:17 PM
Last Post: deanhystad
  kivy binding issue hammer 8 3,114 Nov-07-2021, 11:34 PM
Last Post: hammer
  How to disable custom button Sancho_Pansa 7 3,574 Dec-04-2020, 02:21 PM
Last Post: buran
  How to disable focus on Frame in Tkinter? szafranji 1 3,054 May-13-2020, 10:45 PM
Last Post: DT2000
  Disable entry field and still see value scratchmyhead 5 5,220 May-11-2020, 08:09 PM
Last Post: menator01
  [Tkinter] Binding Entry box to <Button-3> created in for loop iconit 5 5,021 Apr-22-2020, 05:47 AM
Last Post: iconit
  TkInter Binding Buttons ifigazsi 5 4,573 Apr-06-2020, 08:30 AM
Last Post: ifigazsi
  Making text clickable with binding DT2000 10 5,215 Apr-02-2020, 10:11 PM
Last Post: DT2000
  [Tkinter] Setting Binding to Entry created with a loop? p_hobbs 1 2,098 Nov-25-2019, 10:29 AM
Last Post: Larz60+
  [Tkinter] how can disable menu [About] when Toplevel is active balenaucigasa 0 2,692 Oct-25-2019, 09:49 PM
Last Post: balenaucigasa

Forum Jump:

User Panel Messages

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