我正在尝试启动一个线程窗口,因为我有后台进程正在运行,但是两个相同的窗口在同一个线程下启动,我不知道为什么。我猜这段代码可以优化!谢谢你的建议。你知道吗
6/1:我按照建议做了修改,不在主循环中运行两个窗口,这样就行了。第二件事是,一旦按下按钮,窗口就会被破坏自我毁灭()”,但如果我尝试打开另一个窗口,它将不会打开。在尝试启动新窗口之前,我做了一个检查,只有主线程在运行,所以第一个线程不见了。不知道发生了什么,我无法调试线程运行。你知道吗
from Tkinter import *
import ttk
import threading
import thread
from pdctest.test_lib.hid_util import *
class GUI():
def __init__(self, root):
self.root = root # root is a passed Tk object
self.root.title("GUI--->user instruction window")
self.frame = Frame(self.root)
self.frame.pack()
self.label0 = Label(self.frame, text=" ")
self.label0.configure(width=50, height=1)
self.label0.pack()
self.label = Label(self.frame, text="GUI--->execute a SKP on HS within 3s...")
self.label.configure(width=50, height=1)
self.label.pack()
self.button = Button(self.root, text=" GUI--->then click this button ", command=self.buttonWasPressed, fg = 'black', bg = 'dark orange')
self.button.configure(width=40, height=5)
self.button.pack(pady=25)
def removethis(self):
print("GUI--->in removethis")
self.root.destroy()
def buttonWasPressed(self):
print( "GUI--->inside buttonWasPressed: the button was pressed! " + '\r')
self.removethis()
print("GUI--->leaving buttonWasPressed")
def guiSKP(self):
#root = Tk()
#window = GUI(root)
#root.mainloop()
# modified 6/1, fixes multi-window issue
self.root.mainloop()
class GUI_SKP_Thread(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name
def run(self):
root = Tk()
window = GUI(root)
window.guiSKP()
def launch_SKP_thread():
print("beginning launch_SKP_thread" + '\r')
thread_SKP = GUI_SKP_Thread("GUI_SKP_Thread")
thread_SKP.start()
print("exiting launch_SKP_thread" + '\r')
def whatsRunning():
t = threading.enumerate()
print("--------------->whatsRunning")
print t
if __name__ == '__main__':
launch_SKP_thread()
# trying to launch 2nd window
whatsRunning()
time.sleep(4)
whatsRunning()
launch_SKP_thread()
我不太清楚你想用这个程序做什么。但有一件事我很确定,那就是,您正在尝试在同一主循环中运行两个帧。您在不同的位置定义了两次窗口,这意味着有两个帧/窗口。一次在GUI的guiSKP中,一次在GUI的run\uskp\u线程中。你知道吗
因此,您应该将guiSKP中的文本从:
简单地说:
我添加了self以确保它自己运行根.mainloop()
相关问题 更多 >
编程相关推荐