Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-在上一个线程完成后运行下一个线程_Python_Multithreading_Tkinter_Python Multithreading - Fatal编程技术网

Python-在上一个线程完成后运行下一个线程

Python-在上一个线程完成后运行下一个线程,python,multithreading,tkinter,python-multithreading,Python,Multithreading,Tkinter,Python Multithreading,我正在写一个tkinter应用程序。 我想使用线程来避免tkinter窗口冻结,但实际上我没有找到解决方案 我的代码的快速部分(简化): 从线程导入线程 将tkinter作为tk导入 类应用程序(tk.tk): 定义初始化(自): super()。\uuuu init\uuuuu() search_button=tk.button(self,text='Print',command=self.Running) 搜索按钮网格(行=0,列=0) def funct1(自): 打印('一') def

我正在写一个tkinter应用程序。 我想使用线程来避免tkinter窗口冻结,但实际上我没有找到解决方案

我的代码的快速部分(简化):

从线程导入线程
将tkinter作为tk导入
类应用程序(tk.tk):
定义初始化(自):
super()。\uuuu init\uuuuu()
search_button=tk.button(self,text='Print',command=self.Running)
搜索按钮网格(行=0,列=0)
def funct1(自):
打印('一')
def funct2(自我):
打印('Two')
def CreateThread(自身,项目):
self.item=项目
t=线程(目标=自项目)
t、 开始()
def运行(自):
self.CreateThread(self.funct1)
#如何等待self.CreateThread(self.funct1)的结束?
self.CreateThread(self.funct2)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
myGUI=App()
myGUI.mainloop()
如何等待self.CreateThread(self.funct1)结束,然后再运行self.CreateThread(self.funct2)

排队

还有别的吗

我已经看过Thread.join()了,但是它释放了tkinter窗口


希望您能帮助我:)

您可以使用
锁同步线程。如果不知道这些线程需要做什么,很难给出具体的答案。但是,锁可能会解决您的问题。这里有一个关于同步线程的示例。

您可以使用
锁来同步线程。如果不知道这些线程需要做什么,很难给出具体的答案。但是,锁可能会解决您的问题。这里有一个关于同步线程的示例。

在我看来,您应该以不同的方式思考“线程”的含义。线程不是你运行的东西。线程是运行代码的东西。您有两个任务(即,需要完成的事情),您希望这些任务按顺序执行(即,一个接一个)

按顺序做事的最好方法是在同一个线程中进行。与其创建两个单独的线程,为什么不创建一个线程,首先调用
funct1()
,然后调用
funct2()


注:这可能是个错误:

def CreateThread(self, item):
    self.item = item
    t = Thread(target=self.item)
    t.start()
问题是,两个线程都将分配并使用相同的
self.item
属性,第一个线程写入的值可能会在第一个线程使用之前被第二个线程重写。为什么不干脆这样做呢

def CreateThread(self, item):
    Thread(target=item).start()

或者,既然函数体简化为一行,显然创建并启动了一个线程,为什么还要费心定义
CreateThread(…)
。线程不是你运行的东西。线程是运行代码的东西。您有两个任务(即,需要完成的事情),您希望这些任务按顺序执行(即,一个接一个)

按顺序做事的最好方法是在同一个线程中进行。与其创建两个单独的线程,为什么不创建一个线程,首先调用
funct1()
,然后调用
funct2()


注:这可能是个错误:

def CreateThread(self, item):
    self.item = item
    t = Thread(target=self.item)
    t.start()
问题是,两个线程都将分配并使用相同的
self.item
属性,第一个线程写入的值可能会在第一个线程使用之前被第二个线程重写。为什么不干脆这样做呢

def CreateThread(self, item):
    Thread(target=item).start()
或者,既然函数体简化为一行,显然创建并启动了一个线程,为什么还要费心定义
CreateThread(…)