需要 Python TKinter 线程帮助

标签 python multithreading user-interface tkinter

我正在尝试创建一个与 RS232 串行对象通信的 GUI。我将针对我面临的问题提出一个类似的场景。我想创建一个带有 2 个按钮(开始和停止)的框架。开始按钮调用“foo”函数:

status = True

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

这个 foo 函数会一直运行,直到我按下停止键。

def getStatus():
   return status

def stop():
   status = False

我知道 Tkinter 是单线程的,一旦我按“开始”,GUI 将卡住。我知道这可以通过 after 函数实现,但我严格想使用线程。这可以通过线程实现吗?如果可以,您能提供示例代码吗?谢谢。

最佳答案

这是一些(尚不完美)代码:

缺少/损坏了什么,但您没有要求这个,我添加了链接:

  • 它不使用锁 => 对 set 的调用可能会中断,因为它们可能同时发生。 read the docs (这很简单)
  • 它从另一个线程更新 GUI。请参阅1 2
  • 可能更多(不是线程大师)

也可以停止线程查看 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()

关于需要 Python TKinter 线程帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12147945/

相关文章:

java - 当一个单独的线程正在 hibernate 时关闭 java 应用程序是否不好?

Android - 在线程之间传递数据

user-interface - Haskell 和状态

Python matplotlib 圆环图在一个楔形上具有较小的宽度

c - 为实时时钟提供自己的线程是一个好主意吗?

android - 滑卡设计

c# - 避免更新数据后TreeView的折叠效果

python - 重命名多索引列不起作用

python - 将数值数据更改为分类数据 - Pandas

python - 解析字符串以获取特定字符之前和之后的数字