【问题标题】:Two image in tkinter using PhotoImagetkinter 中使用 PhotoImage 的两个图像
【发布时间】:2020-05-13 15:39:52
【问题描述】:

我正在制作一个使用 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)

有什么想法吗?

【问题讨论】:

标签: python-3.x tkinter photoimage


【解决方案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)

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    相关资源
    最近更新 更多