在我的代码中,我试图为一个青蛙游戏制作一个加载屏幕,但是由于某些原因,我遇到了一个问题,我显示一张图片,然后在上面显示一个标签之前执行.sleep
功能,但是它同时显示了这两个标签,它只在应该的时间后运行代码1秒,有人能帮忙吗?你知道吗
下面是我的代码:
from tkinter import *
import tkinter as tk
import time
window = Tk()
window.geometry("1300x899")
LoadingScreen = PhotoImage(file = "FroggerLoad.gif")
Loading = Label(master = window, image = LoadingScreen)
Loading.pack()
Loading.place(x = 65, y = 0)
time.sleep(1)
FroggerDisplay = Label(master = window, font ("ComicSans",100,"bold"),text = "Frogger")
FroggerDisplay.pack()
FroggerDisplay.place(x = 500, y = 300)
window.mainloop()
在启动
window.mainloop()
之前使用time.sleep(1)
时,仅在1秒后创建窗口,FroggerDisplay
标签将与其同时创建。因此,现在不能使用time.sleep(seconds)
。但是,可以使用
window.after(ms, func)
方法,将time.sleep(1)
和window.mainloop()
之间的所有代码放入函数中。请注意,与time.sleep(seconds)
不同,您必须将window.after
(第一个参数)的时间指定为毫秒以下是编辑的代码:
PS:1)为什么要同时使用
.pack(...)
和.place(...)
方法—Tkinter会忽略第一个方法(.pack(...)
) 2) 最好使用Canvas
小部件来创建一个游戏——不像标签那样,它支持透明性,而且使用更简单。例如:注意:您可能需要更改
Canvas
的坐标。你知道吗相关问题 更多 >
编程相关推荐