Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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_Tkinter - Fatal编程技术网

Python 函数在标签之前被调用,但标签应该先打印出来

Python 函数在标签之前被调用,但标签应该先打印出来,python,tkinter,Python,Tkinter,不知怎的,我的Tkinter代码在显示标签之前调用了函数,但是标签应该放在第一位 def doAction(): global e text = tk.Label(root, text="Processing.") text.grid(row=2, columns=1) main() import tkinter as tk root = tk.Tk() root.title("App") e = tk.Entry(r

不知怎的,我的Tkinter代码在显示标签之前调用了函数,但是标签应该放在第一位

def doAction():
    global e
    text = tk.Label(root, text="Processing.")
    text.grid(row=2, columns=1)
    main()

import tkinter as tk

root = tk.Tk()
root.title("App")

e = tk.Entry(root)
e.grid(row=0, column=0, padx=10, pady=10)
e.focus_set()

button = tk.Button(root, text="Go", command=doAction)
button.grid(row=0, column=3)

root.mainloop()
背后的想法是先显示“Processing”,然后让函数“main()”完成它的工作。

一旦它进入
main()
,就会有东西阻塞主线程,因此
mainloop()
无法更新。在调用
main()
之前,您必须强制更新它


无论您在
main()
中做什么,都不要冻结GUI,如果它没有与
tkinter
相关的代码,您可以为
main()

启动一个新线程
main()
运行多长时间?
main()
内有什么?你能把它放在另一个线程中吗?这非常有效。我会复制粘贴main()函数,但它有250行代码。函数main()用于刮除Twitter并清除废弃的数据。@user14903989 Hmmmm最好将其放入新线程中
def doAction():
    .....
    text.grid(row=2, columns=1)

    root.update() # Now the event loop will start processing before the function ends
    main()