Apr-11-2021, 11:04 AM
(Apr-11-2021, 04:35 AM)ndc85430 Wrote: Line 37 in the first code sample: why do you use a lambda to return the functionfinal_count
? Don't you just want thefinal_count
function to be bound to the keypress event? In that case, you should just be passingfinal_count
tobind
.
(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?