2

我正在尝试开始使用 tkinter 来设计一些图形界面,但我实际上有一个问题......

这是我的小代码:

    #!/usr/bin/python3
# -*- coding: utf-8 -*-


# imports
from premier import *
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from time import *


# init var
varstop=False


# functions
def launch():
    """search first numbers between 2 and var entry"""
    listbox.delete(0, "end") # clear listbox
    itime=time() # init time
    n=entry.get() # get limit

    if n.isnumeric()==False or int(n)<2:
        # check inputs
        messagebox.showwarning("", "insert an integer (>=2) on the entry")  
    else:
        stop_button["state"]="normal"
        launch_button["state"]="disabled"

        n=int(n)
        i=2
        nb=0 # count number of first numbers
        varprogress=0 # var of the progress bar
        global varstop

        while i<=n:
            if varstop==True:
                # if click on stop button, stop
                varstop=False
                return
            else:
                if premier(i)==True:
                    nb+=1
                    listbox.insert("end", " "+str(i))
                varprogress=int((i*100/n)-varprogress+1)
                progress.step(varprogress)
                i+=1

        listbox.insert("end", " number of first number between 1 and "+str(n)+": "+str(nb))
        temp=time()-itime
        listbox.insert("end", " process time: "+str(int(temp))+"s")
        stop_button["state"]="disabled"
        launch_button["state"]="normal"

def stop():
    global varstop
    varstop=True


# define gui
gui=Tk()
gui.title("Premier.py")

frame1=Frame(gui)
frame2=Frame(gui)
frame3=Frame(gui)

label=Label(frame1, text="Limit (>=2) :")
listbox=Listbox(frame2, height=25, width=50, selectmode="extended")
progress=ttk.Progressbar(frame3, length=350)
entry=Entry(frame1, width=25)
launch_button=Button(frame3, width=10, text="launch", command=launch)
stop_button=Button(frame3, width=10, text="stop", state="disabled")

# create gui
frame1.pack(expand=True)
frame2.pack(expand=True)
frame3.pack(expand=True)

label.pack(side="left")
entry.pack(side="right")
listbox.pack(side="left")
progress.pack(pady=5)
launch_button.pack(side="left", padx=50, pady=5)
stop_button.pack(side="right", padx=50, pady=5)

gui.mainloop()

当我单击启动按钮时,GUI 只是冻结,直到启动功能的“while”结束,我不知道为什么。所以,例如,我无法管理我的进度条......

是一个小视频...

你知道为什么 GUI 会这样冻结或者我如何解决这个问题吗?提前致谢

4

1 回答 1

0

“为什么”是因为 Tkinter 是单线程的。当事件触发回调时,该回调必须完成,然后才能处理更多事件。

为避免这种情况,您必须:

  1. 将耗时的代码移到另一个线程,或者
  2. 将您的时间密集型代码移至另一个进程,或
  3. 将您的时间密集型代码重构为可以通过事件循环在小迭代中完成的小块,或者
  4. 作为一种低效且可能危险的解决方法,update_idletasks在循环的每次迭代后调用,这至少允许处理“空闲”事件(例如重绘 GUI)
于 2013-07-03T11:37:08.187 回答