DEV Community


Posted on

Start Stop Button in Python

I am a Technical Support Engineer who always quenches his thirst of coding to automate the repeating manual work. I would like to share one of the challenges that I faced while creating one such tool.

I have developed a code to monitor case & ticket in the Ticketing Tool. One of the boring work of a support engineer. The aim was to create a UI for start and stop button which can control this ticketing tool. The UI was created using the Tkinter package.

The Challenge was whenever the start button was pressed, the button won't release until the code execution of the function started using the button completes. During the search for this solution, I came across the MultiThreading.

These guys saved my life. Although this common to many, coming across such thing was a thrill.

Multithreading is having different thread under the same process where all threads share the same memory. So the process always has a Main Thread which is the main program and other thread which is created from Main Thread.

I created a function to start_thread to start my start function as a separate thread

start_btn = Tk.Button(root,Text="Start",command=start_thread
 def start_thread():
    t1= threading.Thread(target=start)

def start():
    while status:
        <Main code>
Enter fullscreen mode Exit fullscreen mode

As we are using Multithreading the memory is shared, so at anytime the value of status can be changed and the application can be stopped

stop_btn = Tk.Button(root,Text="Start",command=start_thread

def stop():
Enter fullscreen mode Exit fullscreen mode

So I can now easily control my application a few lines of code. Though is a small thing for a coder, for me at that moment was a Saviour.

Top comments (1)

villainprod profile image
Brian Richey 🐦

The only issue I see here is your stop button using the "start_thread" command.