简体   繁体   中英

Show Progress Bar during a system call in tkinter

I try to get some input from the user. I make a system call and pass the input as an argument on a button press (Next in this case). During this time I would like to add the indeterminate progress bar widget in the current window until the system call return something and gets into the next function. Somehow progress bars doesn't shows up and I see the next window itself. Below is the code for the same.

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

I also tried to create a new window on the next button press and add a progress bar in that window. But that also did not work. The new window never appeared and I was directly transferred to the next window.

I want to see the progress bar on the button press until the system call is executed and we get to the next window. The progress bar can be in the current window or a new window. If its a new window it should be closed when we get to the next step.

I was able to solve the progress bar freezing on my applications by multithreading the "call" function. So in your code, it would look like:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM