Python Forum
[Tkinter] bind lambda keypress counter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] bind lambda keypress counter
#7
(Apr-11-2021, 04:35 AM)ndc85430 Wrote: Line 37 in the first code sample: why do you use a lambda to return the function final_count? Don't you just want the final_count function to be bound to the keypress event? In that case, you should just be passing final_count to bind.

(Apr-11-2021, 10:16 AM)Yoriz Wrote: By using classes the attributes can be accessed without using global.
Add logic to the button press event to reset the counter at zero.
import tkinter as tk


INITIAL_COUNTER_VALUE = 500


class TkApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.geometry("640x400")
        self.configure(bg='black')
        self.title("Sentry Terminal")
        self.counter = tk.IntVar()
        self.counter.set(INITIAL_COUNTER_VALUE)
        tk.Grid.rowconfigure(self, 0, weight=1, uniform='a')
        tk.Grid.columnconfigure(self, 0, weight=1, uniform='a')
        tk.Grid.rowconfigure(self, 1, weight=1, uniform='b')
        tk.Grid.columnconfigure(self, 1, weight=1, uniform='b')
        rounds = tk.Button(self, justify=tk.CENTER, textvariable=self.counter)
        rounds.grid(row=0, column=0, sticky="EW")
        label1 = tk.Label(self, textvariable=self.counter)
        label1.grid(row=1, column=0, sticky="EW")
        self.bind("<KeyPress-f>", self.on_keypress_f)

    def on_keypress_f(self, evet):
        counter_value = self.counter.get()
        counter_value = counter_value-1 or INITIAL_COUNTER_VALUE
        self.counter.set(counter_value)


tk_app = TkApp()
tk_app.mainloop()

I am still wrapping my head around things. Is there any documentation you can refer me to to read up on so I might get properly educated?
Reply


Messages In This Thread
bind lambda keypress counter - by knoxvilles_joker - Apr-11-2021, 01:02 AM
RE: tkinter bind lambda keypress counter - by Yoriz - Apr-11-2021, 10:16 AM
RE: tkinter bind lambda keypress counter - by knoxvilles_joker - Apr-11-2021, 11:04 AM
RE: tkinter bind lambda keypress counter - by Yoriz - Apr-11-2021, 10:57 PM
RE: bind lambda keypress counter - by Yoriz - Apr-12-2021, 06:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] bind menator01 1 1,276 Apr-15-2022, 08:47 PM
Last Post: menator01
  update text variable on label with keypress knoxvilles_joker 3 4,995 Apr-17-2021, 11:21 PM
Last Post: knoxvilles_joker
  [Tkinter] Mouse click without use bind ATARI_LIVE 8 7,593 Oct-23-2020, 10:41 PM
Last Post: ATARI_LIVE
  [Kivy] AttributeError: 'NoneType' object has no attribute 'bind' faszination_92 2 6,326 Apr-12-2020, 07:01 PM
Last Post: Larz60+
  [WxPython] Bind error PeterLinux 1 2,272 Apr-06-2020, 03:07 AM
Last Post: joe_momma
  Tkinter:Unable to bind and unbind function with a button shallanq 2 5,078 Mar-28-2020, 02:05 AM
Last Post: joe_momma
  [Tkinter] How to bind an event when enter is pressed on a Entry control? Michael4 4 3,993 Aug-29-2019, 10:11 PM
Last Post: Michael4
  Update plot by <Return> bind with entry widget Zorro 1 4,177 Mar-09-2019, 12:27 PM
Last Post: Zorro
  Bind only fires off once? WuchaDoin 3 4,531 Dec-18-2018, 07:46 PM
Last Post: buran
  [WxPython] bind label and entry text with return key metulburr 1 3,307 Aug-14-2018, 10:02 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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