PythonTkinter-用于显示和执行不同进程的多个窗口
我有一个GUI应用程序。它有多个窗口。主窗口每秒更新一次新值。我在这里使用线程和队列。主窗口中有一个按钮。按下它将打开一个新窗口。我想要的是,当显示新窗口时,停止主窗口中的进程(更新值进程),一些新进程将显示在新窗口中。关闭新窗口应再次撤消主窗口并继续此过程。我需要使用多线程还是多处理? 不幸的是,我第二次问这个问题。对不起PythonTkinter-用于显示和执行不同进程的多个窗口,python,tkinter,Python,Tkinter,我有一个GUI应用程序。它有多个窗口。主窗口每秒更新一次新值。我在这里使用线程和队列。主窗口中有一个按钮。按下它将打开一个新窗口。我想要的是,当显示新窗口时,停止主窗口中的进程(更新值进程),一些新进程将显示在新窗口中。关闭新窗口应再次撤消主窗口并继续此过程。我需要使用多线程还是多处理? 不幸的是,我第二次问这个问题。对不起 import tkinter import time import threading import random import queue class GuiPart:
import tkinter
import time
import threading
import random
import queue
class GuiPart:
def __init__(self, master, queue, endCommand,newWindow):
self.queue = queue
self.pause = False
# Set up the GUI
console = tkinter.Button(master, text='Done', command=endCommand)
console.pack()
console2 = tkinter.Button(master, text='New', command=newWindow)
console2.pack()
self.output = tkinter.StringVar()
#output.set(1)
output_1_label = tkinter.Label(master, textvariable= self.output, height=2, width=12)
output_1_label.pack()
# Add more GUI stuff here
self.temp_process()
def temp_process(self):
if not self.pause:
print ("handling messages")
else:
print ("Not handling messages")
root.after(1000,self.temp_process)
def processIncoming(self):
while self.queue.qsize():
try:
msg = self.queue.get(0)
print (msg)
self.output.set(msg)
except queue.Empty:
pass
class ThreadedClient:
def __init__(self, master):
self.master = master
# Create the queue
self.queue = queue.Queue()
# Set up the GUI part
self.gui = GuiPart(master, self.queue, self.endApplication,self.create_window)
self.running = 1
self.thread1 = threading.Thread(target=self.workerThread1) #this is for sending data to queue.
# what about second window?
self.thread1.start()
self.periodicCall()
def on_quit(self):
self.gui.pause = False
self.window.destroy()
def create_window(self):
self.window = tkinter.Toplevel(root)
self.gui.pause = True
self.window.protocol("WM_DELETE_WINDOW",self.on_quit)
def periodicCall(self):
self.gui.processIncoming()
if not self.running:
import sys
sys.exit(1)
self.master.after(1000, self.periodicCall)
def workerThread1(self):
while self.running:
time.sleep(rand.random() * 1)
msg = rand.random()
self.queue.put(msg)
def endApplication(self):
self.running = 0
rand = random.Random()
root = tkinter.Tk()
client = ThreadedClient(root)
root.mainloop()
您可以让您的函数
processIncoming
检查标志,并在需要时暂停/恢复
class GuiPart:
def __init__(self, master, queue, endCommand,newWindow):
self.queue = queue
self.pause = False
....
def processIncoming(self):
while self.queue.qsize() and not self.pause:
try:
msg = self.queue.get(0)
print (msg)
self.output.set(msg)
except queue.Empty:
pass
谢谢你的回答。实际上,我在线程中有一些复杂的过程。我没有将其添加到帖子中以减少代码大小。让我试试你的代码。你为什么说线程什么都没做<代码>def workerThread1(self):正在生成一个随机数,并通过队列将其发送到主gui。我将在以后的案例中使用它在GUI上显示它(使用
tkinter.StringVar()
)。当我按下“新建”按钮时,它应该在主窗口中停止,而另一个进程应该在新窗口屏幕中发生(为了演示,我只是尝试在这个新窗口上发送并显示一个随机数)。我运行了您的代码,但没有输出,因此在您的最小示例中不需要它们。此外,您的问题实际上与线程无关
——您只希望主进程在新窗口打开和关闭时暂停
和恢复
,不是吗?我在帖子中试图减少代码。所以我想我错过了一些内容。实际上,我正在使用此代码在GUI屏幕中显示一些实时传感器数据。在我的实际代码中,由于有一个while循环,我应该使用线程来避免GUI冻结。为了测试传感器,我需要另一个窗口。这就是我想要创造的。这个新窗口应该暂停工作的主窗口。我发布了完整的代码。请你再试一次好吗。已经两天了,我正在努力修复它。!!