1

我正在制作一个使用 2 个不同图像的程序。我有加载页面,它只是一张名为“loading.png”的图片,效果很好。

当我尝试使用相同的方法将名为“table.png”的不同图像添加到不同的窗口时,它不会显示,但在介绍窗口中会显示我想要的图像。

这是加载屏幕代码

win=Tk()
win.title('PokerChamp')
win.geometry('400x200')

background_image = PhotoImage(file='loading.png')
background_label = Label(win, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

这工作正常,但是当我添加此代码时,它显示错误的图像并且主窗口的图像不显示。

root=Tk()
root.withdraw()
root.config(bg='#1b800b')
root.title('PokerChamp')

image = PhotoImage(file='table.ppm')
label = Label(win, image=image)
label.place(x=0, y=0)

有任何想法吗?

4

1 回答 1

1

首先,就像@AD WAN 所说,必须实例化Tk(). 其次,当您启动第二个图像时,您将其放在第一个图像中win,而不是root

这将是您的代码:

from tkinter import *
win=Tk()
win.title('PokerChamp')
win.geometry('400x200')

background_image = PhotoImage(file='loading.png')
background_label = Label(win, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

root=Toplevel()

#root.config(bg='#1b800b')
root.title('PokerChamp')

image = PhotoImage(file='table.ppm')
label = Label(root, image=image)
label.place(relx=0, rely=0)

希望这可以帮助!

于 2020-05-13T21:49:54.900 回答