我想开始使用Tkinter的设计一些图形界面,但实际上我有一个问题...的Tkinter - 按钮命令和GUI块
这是我的一点代码:
#!/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”结束,我不知道为什么。 所以,我管不了我,例如进度...
Here是一个小视频......
你知道为什么GUI冻结一样,或者我怎么能解决这个问题? 在此先感谢
可能重复的[Tkinter - >程序冻结durint函数的执行](http://stackoverflow.com/questions/10847626/tkinter-program-freezing-durint-the-execution-of -a-function) –