Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8469383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:12:45+00:00 2026-06-10T16:12:45+00:00

I am trying to create a GUI that communicates with a RS232 serial object.

  • 0

I am trying to create a GUI that communicates with a RS232 serial object. I’ll present an analogous scenario to the problem that I am facing. I want to create a frame with 2 buttons, Start and Stop. The start button calls the ‘foo’ function:

status = True

def foo():
   n = 0
   while(getStatus()):
       print n
       n += 1
       sleep(0)

This foo function keeps running until I press stop.

def getStatus():
   return status

def stop():
   status = False

I understand Tkinter is single-threaded and once I press ‘Start’, the GUI will freeze. I know this is possible with the after function, but i strictly want to use threading. Is this possible with threading? If so can you please provide a sample code? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:12:47+00:00Added an answer on June 10, 2026 at 4:12 pm

    here is some (not yet perfect) code:

    What is missing/broken, but you did not ask for this, I added links:

    • It does not use locks => the calls to set might brake since they can occur at the same time. read the docs (this is quite easy)
    • It updates the gui from another thread. see 1 2
    • possibly more (not a threading guru)

    Also for stopping threads look here

    import time
    import tkinter
    from tkinter import ttk
    import threading  
    
    #gui
    root = tkinter.Tk()
    root.title("Threading demo")
    
    status = tkinter.StringVar()
    elapsed = tkinter.StringVar()
    error = tkinter.StringVar()
    
    #thread
    class timer(threading.Thread):
        def __init__(self):
            super().__init__()
            self.stopped = False
            #your code here, don't need init if you have no code
    
    
    
        def run(self):
            status.set('running')
    
            while not self.isStopped():
                time.sleep(1)
    
                try:
                    oldtime = int(elapsed.get())
                except ValueError:
                    oldtime = 0
    
                elapsed.set(oldtime+1)
    
            status.set('stopped')
            time.sleep(2)
    
        def isStopped(self):
            return self.stopped
    
    
    
        def stop(self):
            self.stopped = True
    
    
    
    #starts/stops thread (manages it)
    class threadedOp(object):
        def __init__(self):
            self.thread = None
    
    
        def run(self):
            if self.thread == None:
                self.thread = timer()
                status.set('starting')
                self.thread.start()
            else:
                error.set('Thread already running')
    
    
        def stop(self):
            if self.thread != None:
                status.set('stopping')
                self.thread.stop()
                self.thread.join()
                error.set('Join complete')
                self.thread = None
            else:
                error.set('No thread to stop')
    
    op = threadedOp()
    
    #remaining gui
    mainframe = ttk.Frame(root, padding="3 3 12 12")
    mainframe.grid(column=0, row=0, sticky=(tkinter.N, tkinter.W, tkinter.E, tkinter.S))
    mainframe.columnconfigure(0, weight=1)
    mainframe.rowconfigure(0, weight=1)
    
    ttk.Label(mainframe, textvariable=elapsed).grid(column=1, row=1, sticky=(tkinter.W, tkinter.E))
    ttk.Label(mainframe, textvariable=status).grid(column=2, row=1, sticky=(tkinter.W, tkinter.E))
    ttk.Label(mainframe, textvariable=error).grid(column=1, row=3, sticky=(tkinter.W, tkinter.E))
    ttk.Button(mainframe, text="Start", command=op.run).grid(column=1, row=2, sticky=tkinter.W)
    ttk.Button(mainframe, text="Stop", command=op.stop).grid(column=2, row=2, sticky=tkinter.W)
    
    root.mainloop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a responsive gui, which basically means that I have an
I'm trying to create a GUI with Swing. My problem is, I have a
I am trying to create a GUI that allows someone to set up an
I'm trying create a gui using Tkinter that grabs a username and password and
I'm trying to create a Tkinter GUI that saves all entry box values into
I am trying to create a simple GUI that simulates a record store. I
I'm trying to create a dialog that uses two GUI elements: KDEUI KActionSelector and
I'm trying to create an exit button that correctly closes the GUI I have
I'm trying to create GUI program that generates HTML invoices, and sends them for
I'm currently trying to create a gui for a game that I am making

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.