我一直试图让这个线程起作用,但我不明白为什么它不起作用。我正在运行一个非恶意的机器人,它执行一些任务,我写了一个小的GUI供它使用。我如何将这些线程组合在一起?这就是我尝试过而且没有用的东西:
主要机器人代码;
try:
gui_thread = threading.Thread( target = execfile, args = ('GUI.py',) )
gui_thread.start()
except:
print('Unable to load GUI')
GUI似乎加载,因为除了没有运行,它继续按计划加载机器人,但GUI没有像在主GUI.py scrpit中那样显示
GUI代码;
import Tkinter
class Geekster_Bot(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
self.entry.grid(column=0,row=0,sticky='EW')
self.entry.bind("<Return>", self.OnPressEnter)
self.entryVariable.set("")
button = Tkinter.Button(self,text="Update!",
command=self.OnButtonClick)
button.grid(column=1,row=0)
button2 = Tkinter.Button(self,text="Clear Whitelist",
command=self.OnButtonClick_Whitelist_Wipe)
button2.grid(column=2,row=0)
self.labelVariable = Tkinter.StringVar()
label = Tkinter.Label(self,textvariable=self.labelVariable,
anchor="w",fg="white",bg="blue")
label.grid(column=0,row=1,columnspan=2,sticky='EW')
self.labelVariable.set("Select Modpack!")
self.grid_columnconfigure(0,weight=1)
self.resizable(True,False)
self.update()
self.geometry(self.geometry())
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def OnButtonClick(self):
try:
modpackdoc = open(r'E:\Geekster_Bot\Modpack.txt', 'w')
modpackdoc.write(self.entryVariable.get())
modpackdoc.close()
self.labelVariable.set("Updated Modpack: " +self.entryVariable.get())
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
except:
self.labelVariable.set("Unable to Update the Modpack file!")
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def OnPressEnter(self,event):
try:
modpackdoc = open(r'E:\Geekster_Bot\Modpack.txt', 'w')
modpackdoc.write(self.entryVariable.get())
modpackdoc.close()
self.labelVariable.set("Updated Modpack: " +self.entryVariable.get())
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
except:
self.labelVariable.set("Unable to Update the Modpack file!")
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def OnButtonClick_Whitelist_Wipe(self):
try:
execfile('command_whitelist_wipe.py')
self.labelVariable.set("Whitelist Cleared")
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
except:
self.labelVariable.set("Unable to Wipe Whitelist!")
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
if __name__ == "__main__":
app = Geekster_Bot(None)
app.title('Geekster_Bot')
app.geometry('450x100')
app.mainloop()
两者都没有任何问题! GUI在线程化时根本不会加载一个窗口!
提前致谢!
答案 0 :(得分:1)
如果我理解正确,这是合适的解决方案(更改您的主机器人代码):
import threading
import subprocess
def create_giu():
subprocess.call(['python', 'GUI.py'])
try:
t1 = threading.Thread(target=create_gui)
t1.start()
except:
print('Unable to load GUI')
#continue execution
print 1234
但是,我认为这在软件架构方面是一个糟糕的解决方案。
答案 1 :(得分:0)
GUI应该在程序的主线程中,它不能从另一个线程执行。在后一种情况下,它位于主线程中,而在前者中,如果来自不是主线程的线程,则尝试启动。