如果我打包在命令中,为什么tkinter不显示图像

时间:2019-04-26 20:49:06

标签: python-3.x tkinter

我想打包图像供按钮按下(命令)。但是,当我按下按钮时,标签会显示出来,但是它是空的

from tkinter import *

def y():

    x = PhotoImage(file= "M7.png")
    image = Label(root, image=x).pack()

root=Tk()
Button(root,text="IDK" ,command=y).pack()
root.mainloop()

我想在纸牌游戏中用作纸牌查看器。这是一个学校项目,这就是为什么我使用tkinter和python。 先前的疑问我不理解,这就是为什么我再次询问。

1 个答案:

答案 0 :(得分:0)

此行image = Label(root, image=x).pack()将返回None值,因为pack返回None,您可以通过打印比较它并查看差异。

image = Label(root, image=x).pack() 
print(image)
image = Label(root, image=x)
print(image)

这是您的代码

from tkinter import *

def y():

    x = PhotoImage(file= "M7.png")
    image = Label(root, image=x)
    image.pic = x
    image.pack()

root=Tk()
Button(root,text="IDK" ,command=y).pack()
root.mainloop()