单击菜单栏按钮时,主GUI冻结并停止计数

时间:2018-12-03 06:57:39

标签: python multithreading tkinter

我的问题是,当我单击菜单栏按钮时,GUI主窗口中的计数会冻结,直到按下按钮为止。在方法之后,我已经阅读了很多有关线程和回调的信息,但是这些都无助于解决此问题。除非我缺少任何东西。请让我知道问题可能在哪里。谢谢!

from tkinter import*
import time
import _thread as thread

def NewFile():
    print ("New File!")
def OpenFile():
    showerror('Not implemented','Not yet available')
def About():
    print ("This is a simple example of a menu")

def my_func():

    for count in range(1000000):

        count = count + 1

        entry.delete(0,END)
        entry.insert(0, count)
        entry.update()  

def menu(root):             
    menu = Menu(root)
    root.config(menu=menu)
    filemenu = Menu(menu)
    menu.add_cascade(label="File", menu=filemenu)
    filemenu.add_command(label="New", command=NewFile)
    filemenu.add_command(label="Open...", command=OpenFile)
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command=root.destroy)
    helpmenu = Menu(menu)
    menu.add_cascade(label="Help", menu=helpmenu)
    helpmenu.add_command(label="About...", command=About)


root = Tk()
root.geometry("400x400")


lab = Label(root, text="Date and Time", width=7, relief=SUNKEN, bg = "yellow", font = "times 16 bold")
lab.pack(side=TOP, fill=X,  padx=5, pady=5)
entry = (Entry(root, relief=SUNKEN, bg = "powder blue", fg="blue", justify='center', font = "times 16 bold"))
entry.insert(0,"0")
entry.pack(padx=5, pady=5)

root.after(20, my_func)
menu(root)  
root.mainloop()

0 个答案:

没有答案