我正在尝试开始使用 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 会这样冻结或者我如何解决这个问题吗?提前致谢