Python Forum
Show the result of every count
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Show the result of every count
#5
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()
George87 likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
Show the result of every count - by George87 - Dec-28-2021, 11:42 AM
RE: Show the result of every count - by menator01 - Dec-28-2021, 01:48 PM
RE: Show the result of every count - by George87 - Dec-28-2021, 02:17 PM
RE: Show the result of every count - by menator01 - Dec-28-2021, 03:36 PM
RE: Show the result of every count - by menator01 - Dec-28-2021, 04:00 PM
RE: Show the result of every count - by George87 - Dec-28-2021, 05:16 PM
RE: Show the result of every count - by deanhystad - Dec-28-2021, 05:33 PM
RE: Show the result of every count - by menator01 - Dec-28-2021, 06:04 PM
RE: Show the result of every count - by deanhystad - Dec-28-2021, 08:58 PM
RE: Show the result of every count - by menator01 - Dec-28-2021, 09:04 PM
RE: Show the result of every count - by deanhystad - Dec-28-2021, 10:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] How to get the result of a ping to show in tkinter? jacklee26 6 7,959 Feb-10-2023, 01:12 PM
Last Post: NebularNerd

Forum Jump:

User Panel Messages

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