Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was trying to create a counter gui and from what I've tried it was working but I need to use between the 3 concepts the Threading, Concurrency and Parallelism. Just want to ask what would be the best or simplest? (will insert/edit the code after)

What I have tried:

from tkinter import *

window = Tk()
window.geometry('300x100')
window.title('Counter')
window.resizable(0, 0)

counter = -1
running = False


def counter_label(lbl):
    def count():
        if running:
            global counter
            if counter == -1:
                display = "0"
            else:
                display = str(counter)

            lbl['text'] = display

            lbl.after(1000, count)
            counter += 1

    count()


def StartTimer(lbl):
    global running
    running = True
    counter_label(lbl)
    start_btn['state'] = 'disabled'
    stop_btn['state'] = 'normal'


def StopTimer():
    global running
    start_btn['state'] = 'normal'
    stop_btn['state'] = 'disabled'
    running = False


lbl = Label(window, text="0", width=8)
lbl.grid(column=4, row=2)
lbl.place(x=170, y=15)
label_msg = Label(window, text="Counter")
label_msg.place(x=100, y=15)

start_btn = Button(window, text='Start Counting', width=16, command=lambda: StartTimer(lbl))
start_btn.grid(column=2, row=3)

stop_btn = Button(window, text='Stop Counting', width=16, state='disabled', command=StopTimer)
stop_btn.grid(column=5, row=3)


start_btn.place(x=25, y=40)
stop_btn.place(x=155, y=40)


window.mainloop()
Posted
Comments
[no name] 3-Jun-21 11:57am    
When it comes to UI's, it's responsiveness; which implies asynchronous. The display is slower than the CPU; which implies a separate counting thread; unless the object is to show each count. But if you needed multiple counters than you could go for parallelism. And if they all updated the same counter (without crashing), you have concurrency.
lil_mint 5-Jun-21 1:50am    
Thank you very much for the explanation...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900