我正在用 Python 开发一个 Tkinter 应用程序。我有两个后台操作和一个用户需求操作。这是我的示例代码:
from threading import Thread
import tkinter as tk
import time
class Controller(object):
def __init__(self, master):
self.master = master
self.btn1 = tk.Button(self.master, text="Start Recording", width=16, height=5, command=lambda: self.start_background_opt())
self.btn1.grid(row=2, column=0)
self.btn3 = tk.Button(self.master, text="Fly", width=16, height=5, command=lambda: self.fly_button())
self.btn3.grid(row=3, column=0)
self.entry = tk.Entry(self.master)
self.entry.grid(row=4, column=0)
self.connect_button_clicked = False
self.thread1 = None
self.thread2 = None
self.thread3 = None
self.flight_completed = False
def background_opt1(self):
while True:
if self.connect_button_clicked:
print("Battery Fetching")
else:
return
def background_opt2(self):
while True:
if self.connect_button_clicked:
print("Signal Fetching")
else:
return
def start_background_opt(self):
if not self.connect_button_clicked:
self.connect_button_clicked = True
self.thread1 = Thread(target=self.background_opt1).start()
self.thread2 = Thread(target=self.background_opt2).start()
else:
self.connect_button_clicked = False
self.thread1 = None
self.thread2 = None
def flight_operation_controller(self):
if self.flight_completed:
self.thread3 = None
def fly_button(self):
self.flight_completed = False
self.thread3 = Thread(target=self.static_sim()).start()
因此,当用户单击“开始录制”时,它会启动 2 个后台操作。他们应该作为后台运行。然后当用户单击“飞行”按钮时,应执行飞行操作。为了不阻塞我的主 UI,我将它们放在单独的线程中。
实际上我的所有操作都正常工作。我已投入time.sleep 复制我的飞行操作;但是当它运行时,它会阻塞我的整个,即使它在单独的线程中运行。
你能告诉我为什么我看到这个吗?我对 Pyhton tkinter 中的多线程的解释是否正确?最好的祝福
人到中年有点甜
相关分类