我有一个python tkinter GUI应用程序。每秒钟显示一些传感器数据。所以我正在使用多线程和队列来更新GUI屏幕(在window-1或主窗口上)上的数据。现在,我想添加一个按钮。然后按此按钮应打开一个新窗口(窗口2)以执行传感器控制的其他操作。此window-2应该停止window-1的操作。有可能这样做吗?是否可以添加诸如关闭窗口-2将撤消并激活窗口1的操作之类的功能?
#Created by Jacob Hallén, AB Strakt, Sweden. 2001-10-17
#I am using this example
import tkinter
import time
import threading
import random
import queue
class GuiPart:
def __init__(self, master, queue, endCommand):
self.queue = queue
# Set up the GUI
console = tkinter.Button(master, text='Done', command=endCommand)
console.place(x=15, y=400)
Button_sensor = tkinter.Button(master, text = "Sensor Control", command=secondWindow)
Button_sensor.place(x=600, y=300)
#the above button should call secondWindow method and how to stop the #activities in the window 1? and go to window-2? and come back to window-1 #after closing window-2
def processIncoming(self):
while self.queue.qsize():
try:
msg = self.queue.get(0)
# Check contents of message and do what it says
# As a test, we simply print it
print (msg)
except queue.Empty:
pass
def secondWindow(self):
# function definition here
# any suggestions?
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.running = 1
self.thread1 = threading.Thread(target=self.workerThread1)
self.thread1.start()
self.periodicCall()
def periodicCall(self):
"""
Check every 1000 ms if there is something new in the queue.
"""
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()