在tkin中的系统调用时显示进度条

2024-04-20 02:25:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着从用户那里得到一些输入。我进行一个系统调用,并将输入作为一个参数传递给一个按钮按下(在本例中是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()

我还试着在next按钮按下时创建一个新窗口,并在该窗口中添加一个进度条。但这也没用。新窗口从未出现,我直接转到下一个窗口。在

我想看到按钮按下时的进度条,直到系统调用执行完毕,我们进入下一个窗口。进度条可以在当前窗口或新窗口中。如果这是一个新窗口,当我们进入下一步时,它应该关闭。在


Tags: the进度条selfmastertitledefrootdetails
1条回答
网友
1楼 · 发布于 2024-04-20 02:25:53

我能够通过多线程的“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)

相关问题 更多 >