Python线程问题

Python线程问题,python,multithreading,user-interface,Python,Multithreading,User Interface,我一直试图让这个线程工作,但我不明白为什么它不工作。我正在运行一个非恶意的bot,它可以执行一些任务,我还为它编写了一个小型GUI。我怎样把这些线穿在一起?这就是我尝试过但没有成功的地方: 主要Bot代码 try: gui_thread = threading.Thread( target = execfile, args = ('GUI.py',) ) gui_thread.start() except: print('Unable to load GUI') GU

我一直试图让这个线程工作,但我不明白为什么它不工作。我正在运行一个非恶意的bot,它可以执行一些任务,我还为它编写了一个小型GUI。我怎样把这些线穿在一起?这就是我尝试过但没有成功的地方:

主要Bot代码

try:
    gui_thread = threading.Thread( target = execfile, args = ('GUI.py',) )
    gui_thread.start()

except:
    print('Unable to load GUI')
GUI显示为在未运行except时加载,并继续按计划加载bot,但GUI的显示方式与主GUI.py scrpit中的显示方式不同

图形用户界面代码

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()
导入Tkinter
Geekster_机器人类(Tkinter.Tk):
定义初始化(自身,父级):
Tkinter.Tk.\uuuuu init\uuuuuu(自,父)
self.parent=parent
self.initialize()
def初始化(自):
self.grid()
self.entryVariable=Tkinter.StringVar()
self.entry=Tkinter.entry(self,textvariable=self.entryVariable)
self.entry.grid(列=0,行=0,sticky='EW')
self.entry.bind(“,self.on按Enter键)
self.entryVariable.set(“”)
button=Tkinter.button(self,text=“Update!”,
command=self.onButton(单击)
button.grid(列=1,行=0)
button2=Tkinter.Button(self,text=“清除白名单”,
command=self.onButton单击\u白名单\u擦除)
按钮2.网格(列=2,行=0)
self.labelVariable=Tkinter.StringVar()
label=Tkinter.label(self,textvariable=self.labelVariable,
anchor=“w”,fg=“白色”,bg=“蓝色”)
label.grid(column=0,row=1,columnspan=2,sticky='EW')
self.labelVariable.set(“选择Modpack!”)
self.grid\u column配置(0,权重=1)
self.reshable(真、假)
self.update()
self.geometry(self.geometry())
self.entry.focus_set()
self.entry.selection_范围(0,Tkinter.END)
def ON按钮单击(自身):
尝试:
modpackdoc=open(r'E:\Geekster\u Bot\Modpack.txt,'w')
modpackdoc.write(self.entryVariable.get())
modpackdoc.close()
self.labelVariable.set(“更新的Modpack:+self.entryVariable.get())
self.entry.focus_set()
self.entry.selection_范围(0,Tkinter.END)
除:
self.labelVariable.set(“无法更新Modpack文件!”)
self.entry.focus_set()
self.entry.selection_范围(0,Tkinter.END)
def OnPressEnter(自我,事件):
尝试:
modpackdoc=open(r'E:\Geekster\u Bot\Modpack.txt,'w')
modpackdoc.write(self.entryVariable.get())
modpackdoc.close()
self.labelVariable.set(“更新的Modpack:+self.entryVariable.get())
self.entry.focus_set()
self.entry.selection_范围(0,Tkinter.END)
除:
self.labelVariable.set(“无法更新Modpack文件!”)
self.entry.focus_set()
self.entry.selection_范围(0,Tkinter.END)
def ON按钮单击\u白名单\u擦拭(自):
尝试:
execfile('command\u whitelist\u wipe.py')
self.labelVariable.set(“清除白名单”)
self.entry.focus_set()
self.entry.selection_范围(0,Tkinter.END)
除:
self.labelVariable.set(“无法擦除白名单!”)
self.entry.focus_set()
self.entry.selection_范围(0,Tkinter.END)
如果名称=“\uuuuu main\uuuuuuuu”:
app=极客机器人(无)
应用标题(“Geekster_机器人”)
应用几何(“450x100”)
app.mainloop()
这两个工作没有任何问题!GUI根本不加载窗口,就像线程化时一样


提前谢谢

GUI应该在程序的主线程中,它不能从其他线程执行。在后一种情况下,它位于主线程中,而在前一种情况下,您尝试从非主线程的线程开始。如果我理解正确,这是合适的解决方案(更改主Bot代码):


但是,就软件架构而言,我认为这是一个糟糕的解决方案。

我从未使用过Tkinter,但Python线程与大多数GUI库不兼容,所以我想Tkinter也是如此。@Valentin Lorentz我以前在Python线程中使用过Tkinter。GUI比这个更基本,工作得非常好。这就是我感到困惑的原因。谢谢你的帮助
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