在tkinter中进行系统调用时显示进度条

时间:2015-06-29 14:41:47

标签: python tkinter call tk

我尝试从用户那里获得一些输入。我进行系统调用并在按下按钮时将输入作为参数传递(在本例中为Next)。在此期间,我想在当前窗口中添加不确定的进度条小部件,直到系统调用返回某些内容并进入下一个函数。不知怎的进度条没有出现,我看到下一个窗口本身。以下是相同的代码。

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()

我还尝试在下一个按钮上创建一个新窗口,并在该窗口中添加一个进度条。但那也行不通。新窗口从未出现,我被直接转移到下一个窗口。

我希望按下按钮上的进度条,直到执行系统调用,然后我们进入下一个窗口。进度条可以位于当前窗口或新窗口中。如果它是一个新窗口,它应该在我们进入下一步时关闭。

1 个答案:

答案 0 :(得分:0)

通过多线程调用" 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)