Click here to Skip to main content
14,970,143 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.resizable(0, 0)

counter = -1
running = False

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

            lbl['text'] = display

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


def StartTimer(lbl):
    global running
    running = True
    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), y=15)
label_msg = Label(window, text="Counter"), 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), y=40), y=40)

Gerry Schmitz 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.
bnhr 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