【发布时间】: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