Another way
#! /usr/bin/env python3 # Do the imports import tkinter as tk import sys # Function for increasing or decreasing the counter # action is an argument that is passed to perform the desired action def doit(action): # doit counter is a static variable to hold the current value doit.counter = getattr(doit, 'counter', 0) # if the action argument equals add, increase the counter # elif the action is equal to sub, decrease the counter value # else reset the counter value to 0 if action == 'add': doit.counter += 1 elif action == 'sub': doit.counter -= 1 # If the counter goes less than 0, set counter to 0 if doit.counter <= 0: doit.counter = 0 else: doit.counter = 0 # Update the text in the label label['text'] = f'Counter: {doit.counter}' app = tk.Tk() app['padx'] = 5 app['pady'] = 3 label = tk.Label(app, text='Counter: 0', anchor='w') label.pack(fill='x') # The lambda is a way to pass arguments to the function call btn1 = tk.Button(app, text='Up', command=lambda: doit(action='add')) btn1.pack(side='left') btn2 = tk.Button(app, text='Down', command=lambda: doit(action='sub')) btn2.pack(side='left') btn3 = tk.Button(app, text='Reset', command=lambda: doit(action='reset')) btn3.pack(side='left') btn4 = tk.Button(app, text='Exit', command=sys.exit) btn4.pack(side='left') app.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags