在 tkinter 的系统调用期间显示进度条
Show Progress Bar during a system call in tkinter
我尝试从用户那里获得一些输入。我进行系统调用并在按下按钮时将输入作为参数传递(在本例中为 Next)。这段时间我想在当前window中添加不确定的进度条小部件,直到系统调用return something并进入下一个函数。不知何故进度条没有显示,我看到了下一个 window 本身。下面是相同的代码。
from Tkinter import *
import ttk
class App:
def __init__(self, master):
#copy the root
self.master = master
#Label for the root
self.title_frame = Frame(self.master)
self.title_label= Label(self.title_frame, text="My application")
#position title label
self.title_frame.pack(fill=X)
self.title_label.pack()
#Create frame containing details
self.detail_frame1 = Frame(self.master)
self.detail_frame2 = Frame(self.master)
#Create entry for input details
self.input1 = Entry(self.detail_frame1)
self.function()
def function(self):
#copy the root window
master = self.master
#copy the body frame
detail_frame = self.detail_frame1
#position the details frame
detail_frame.pack()
#Create the Labels to be displayed in the details frame
input1_label = Label(detail_frame, text="input:")
#button to enter the next window
next = Button(detail_frame, text="Next", width=26,height=2, command= lambda: self.function1())
input1_label.grid(row=0, sticky=E, padx=10, pady=10)
self.input1.grid(row=0, column=2, sticky=W, pady=10)
next.grid(row=3, column=3, pady=5, padx=5)
def function1(self):
pb = ttk.Progressbar(self.detail_frame1, orient='horizontal', mode='indeterminate')
pb.pack()
pb.start(1)
#get the paper code of the paper to be checked
input1 = self.input1.get()
# system call based on the value of input1
call("")
#
self.function2()
def function2(self):
self.detail_frame1.pack_forget()
self.detail_frame2.pack()
def main():
#create the root window
root = Tk()
root.resizable(width=FALSE, height=FALSE)
app = App(root)
root.mainloop()
if __name__=='__main__':
main()
我还尝试在下一次按下按钮时创建一个新的 window,并在该 window 中添加一个进度条。但这也没有用。新的window一直没有出现,我直接转到了下一个window。
我想在按下按钮时看到进度条,直到执行系统调用,然后我们进入下一个 window。进度条可以在当前的window或新的window。如果它是一个新的 window 它应该在我们进入下一步时关闭。
我能够通过 "call" 函数的多线程处理来解决我的应用程序的进度条冻结问题。所以在你的代码中,它看起来像:
import time
import threading
class App:
def function1(self):
pb = ttk.Progressbar(self.detail_frame1, orient='horizontal', mode='indeterminate')
pb.pack()
pb.start(1)
#get the paper code of the paper to be checked
input1 = self.input1.get()
# system call based on the value of input1
t = threading.Thread(target=call, args="")
t.start()
self.function2()
def function2(self):
self.detail_frame1.pack_forget()
self.detail_frame2.pack()
def call():
time.sleep(2)
我尝试从用户那里获得一些输入。我进行系统调用并在按下按钮时将输入作为参数传递(在本例中为 Next)。这段时间我想在当前window中添加不确定的进度条小部件,直到系统调用return something并进入下一个函数。不知何故进度条没有显示,我看到了下一个 window 本身。下面是相同的代码。
from Tkinter import *
import ttk
class App:
def __init__(self, master):
#copy the root
self.master = master
#Label for the root
self.title_frame = Frame(self.master)
self.title_label= Label(self.title_frame, text="My application")
#position title label
self.title_frame.pack(fill=X)
self.title_label.pack()
#Create frame containing details
self.detail_frame1 = Frame(self.master)
self.detail_frame2 = Frame(self.master)
#Create entry for input details
self.input1 = Entry(self.detail_frame1)
self.function()
def function(self):
#copy the root window
master = self.master
#copy the body frame
detail_frame = self.detail_frame1
#position the details frame
detail_frame.pack()
#Create the Labels to be displayed in the details frame
input1_label = Label(detail_frame, text="input:")
#button to enter the next window
next = Button(detail_frame, text="Next", width=26,height=2, command= lambda: self.function1())
input1_label.grid(row=0, sticky=E, padx=10, pady=10)
self.input1.grid(row=0, column=2, sticky=W, pady=10)
next.grid(row=3, column=3, pady=5, padx=5)
def function1(self):
pb = ttk.Progressbar(self.detail_frame1, orient='horizontal', mode='indeterminate')
pb.pack()
pb.start(1)
#get the paper code of the paper to be checked
input1 = self.input1.get()
# system call based on the value of input1
call("")
#
self.function2()
def function2(self):
self.detail_frame1.pack_forget()
self.detail_frame2.pack()
def main():
#create the root window
root = Tk()
root.resizable(width=FALSE, height=FALSE)
app = App(root)
root.mainloop()
if __name__=='__main__':
main()
我还尝试在下一次按下按钮时创建一个新的 window,并在该 window 中添加一个进度条。但这也没有用。新的window一直没有出现,我直接转到了下一个window。
我想在按下按钮时看到进度条,直到执行系统调用,然后我们进入下一个 window。进度条可以在当前的window或新的window。如果它是一个新的 window 它应该在我们进入下一步时关闭。
我能够通过 "call" 函数的多线程处理来解决我的应用程序的进度条冻结问题。所以在你的代码中,它看起来像:
import time
import threading
class App:
def function1(self):
pb = ttk.Progressbar(self.detail_frame1, orient='horizontal', mode='indeterminate')
pb.pack()
pb.start(1)
#get the paper code of the paper to be checked
input1 = self.input1.get()
# system call based on the value of input1
t = threading.Thread(target=call, args="")
t.start()
self.function2()
def function2(self):
self.detail_frame1.pack_forget()
self.detail_frame2.pack()
def call():
time.sleep(2)