【问题标题】:Why is my progress bar does not show up in tkinter为什么我的进度条没有出现在 tkinter
【发布时间】:2021-02-08 12:00:44
【问题描述】:

我正在尝试为我的 Tkinter“应用程序”编写加载屏幕并尝试制作动画进度条。我按照教程将代码改编成我自己的版本。起初,当我单击运行时,代码运行良好,但一旦我添加了一个动画进度条的功能,进度条就完全停止显示了。我最终得到的代码如下:

from tkinter import *
import random
import time
import sqlite3
from tkinter import simpledialog
from tkinter import messagebox
from tkcalendar import *
from tkinter import ttk
import math
from PIL import Image, ImageTk
import winsound

#--------------Frames--------------------------
class VendingApp(Tk):
    def __init__(self):
        Tk.__init__(self)
        self._frame = None
        self.switch_frame(Loading)

    def switch_frame(self, frame_class):
        #Destroys current frame and replaces it with a new one.
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()

class Loading(Frame):    
    def __init__(self, master):
        Frame.__init__(self, master)


    def load():
        for x in range(100):
            Loading_bar['value'] += 1
            root.update_idiletasks()
            time.sleep(1)
##            

        Loading_bar = ttk.Progressbar(self, orient = HORIZONTAL,
                                      length = 500, mode = 'determinate')
        Loading_bar.pack(pady = 200)


        load()
        
#---------------------------------------------------------------------------------------




#----------------------------------------------------------------------------------------
if __name__ == "__main__":
    root = VendingApp()
    #Sets the size of the window
    root.geometry("1024x768")
    #Renames the TITLE of the window
    root.title("Vending machine")
    root.geometry("1024x768")
    root.resizable(False, False) 


    root.mainloop()

我创建了一个单独的文件来尝试查看它是否可以仅使用 tkinter 而没有使用此代码的类:

from tkinter import ttk
from tkinter import*
import time

root = Tk()

def load():
    for x in range(100):
        time.sleep(1)
        Loading_bar['value'] += 1
        root.update_idletasks()
   
    None


Loading_bar = ttk.Progressbar(root, orient = HORIZONTAL,
                            length = 500, mode = 'determinate')
Loading_bar.pack()

load()

root.mainloop()

结果是每次我启动它时,程序都会无限期地加载。有人可以告诉我我做错了什么吗?不需要提供正确的代码,只需提供错误/秒和一些关于如何修复它的指针就可以了,尽管非常感谢正确的代码。谢谢!

【问题讨论】:

    标签: tkinter


    【解决方案1】:

    sleep() 函数将暂停执行并更新 GUI。

    尝试改用after() 函数。它会为以后安排 load() 函数,但不会影响程序的其余部分:

    from tkinter import ttk
    from tkinter import*
    import time
    
    root = Tk()
    
    def load():
        current_value = Loading_bar.cget('value')
        if current_value < 100:
            root.after(10, load)
        Loading_bar.config(value=current_value+1)
    
    Loading_bar = ttk.Progressbar(root, orient = HORIZONTAL,
                                length = 500, mode = 'determinate')
    Loading_bar.pack()
    
    load()
    
    root.mainloop()
    

    添加新示例

    我添加了一个新示例,它是对您的原始代码的修改。我在Loading() 类的__init__() 函数中创建进度条。进度条达到 100 后,进度条会被移除,但不会被销毁。

    from tkinter import *
    from tkinter import ttk
    
    class VendingApp(Tk):
        def __init__(self):
            Tk.__init__(self)
            self._frame = None
            self.switch_frame(Loading)
    
        def switch_frame(self, frame_class):
            new_frame = frame_class(self)
            if self._frame is not None:
                self._frame.destroy()
            self._frame = new_frame
            self._frame.pack()
    
    class Loading(Frame):    
        def __init__(self, master):
            Frame.__init__(self, master)
            self.Loading_bar = ttk.Progressbar(self, orient=HORIZONTAL,
                                               length=500, mode='determinate')
            self.Loading_bar.pack(pady=200)
            self.load()
    
        def load(self):
            current_value = self.Loading_bar.cget('value')
            if current_value < 100:
                self.after(10, self.load)
            else:
                self.Loading_bar.pack_forget()  # Removing widget from frame
            self.Loading_bar.config(value=current_value+1)
    
    if __name__ == "__main__":
        root = VendingApp()
        root.geometry("1024x768")
        root.mainloop()
    

    这样效果更好吗?

    【讨论】:

    • 非常感谢您的回复:) 代码的故障排除行运行良好,但是当我将其应用到应用程序中时,问题仍然存在,我不明白为什么。
    • 我通过修改您的原始代码添加了一个新示例。这是否更好地说明了解决方案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    相关资源
    最近更新 更多