Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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 线程化Tk GUI和cpu密集型数学公式_Python_Tkinter - Fatal编程技术网

Python 线程化Tk GUI和cpu密集型数学公式

Python 线程化Tk GUI和cpu密集型数学公式,python,tkinter,Python,Tkinter,基本上,我正在尝试将线程与TKGUI结合使用,到目前为止,由于cpu密集型任务似乎会使GUI过载,我一直存在GUI冻结问题。我对线程技术非常陌生,在尝试让两者同时工作时遇到了很多问题 所以在后台,一个cpu密集型的数学方程正在运行,在前台,一个GUI弹出,它有一个按钮,允许用户将数据输入到数组中,以备以后使用,我希望这两个过程同时进行。有什么建议吗 from tkinter import * import threading import math class App(Tk): def

基本上,我正在尝试将线程与TKGUI结合使用,到目前为止,由于cpu密集型任务似乎会使GUI过载,我一直存在GUI冻结问题。我对线程技术非常陌生,在尝试让两者同时工作时遇到了很多问题

所以在后台,一个cpu密集型的数学方程正在运行,在前台,一个GUI弹出,它有一个按钮,允许用户将数据输入到数组中,以备以后使用,我希望这两个过程同时进行。有什么建议吗

from tkinter import *
import threading
import math

class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.addWidgets()

    def addWidgets(self):
        self.wordEntry = Entry(self, width = 16)
        self.wordEntry.grid(row = 2, column = 1)
        self.wordEntry.insert(END, "Enter")

        self.addBtn = Button(self, text = "Add", width = 16, command = self.add)
        self.addBtn.grid(row = 2, column = 2)

    def add(self):
        global wordList
        wordList.append(self.wordEntry.get())
        print(wordList)

def startThreads():
    t1 = threading.Thread(target=mathFUNC1, args = (100000,))
    t1.start()

def mathFUNC1():
    #This is what the math function would be essentially

def mathFUN2():
    #This would be called by mathFUNC1

def main():
    startThreads()
    app = App()
    app.wm_title("GUI")

if __name__ == "__main__":
    main()

因此,希望有一种方法可以让数学函数运行,但也可以让应用程序类不冻结,供前台用户使用。

您的代码看起来还可以。您只需要
app.mainloop()
def mathFUNC1(variable)
中的变量。和
wordList=[]
。这不是它是否运行的问题(我大大简化了我的实际代码,使其更易于理解),只是当我运行它时,mathFUNC1和mathFUNC2函数的复杂性非常大,它会导致Tk一次冻结约15秒,我在想办法防止它结冰。