import tkinter as tk
from tkinter import *
from PIL import ImageTk , Image

def addCards():
    img = Image.open("widgetClass\poker2.png")
    img = img.resize((50 , 70) , Image.ADAPTIVE)
    imgTest = ImageTk.PhotoImage(img)

    cards = tk.Label(
        master=frame,
        image=imgTest
    )

    cards.place(x = 20 , y=50)

root = tk.Tk()
root.title("Display images")
root.geometry("400x400")
root.resizable(False , False)

frame = tk.Frame(borderwidth=2 , height=100 , highlightbackground="red" , highlightthickness=2)
frame_b = tk.Frame(borderwidth=2 , height=100 , highlightbackground="red" , highlightthickness=2)

label = tk.Label(frame , text="Picture demo")
button = tk.Button(frame_b , text="Add cards" , command=addCards)


frame.pack(fill=X)
frame_b.pack(fill=X)
label.place(x=0 , y=0)
button.place(x=0 , y=0)

root.mainloop()

我试图使用addCard()函数显示图像,但它只显示一个空标签.我期待每当我按下Add Card按钮时,第一帧都会弹出一个图像,但不幸的是没有显示任何图像.

推荐答案

我期待着每当我按下Add Card按钮时,都会有一个 图像在第一帧中弹出,但未显示图像 不幸的是.

这个问题是可以解决的. 在第20行上: 更改这一点:

cards.place(x = 20 , y=50)

致:

cards.place(x = 2, y=20

在第21行添加以下内容:

def addCards():
     .
     .
     .
     
        
    cards.place(x = 2, y=20)
    cards.image = imgTest #<== Add this.

屏幕截图:

enter image description here

Python相关问答推荐

单击cookie按钮,但结果不一致

不同数据类型的Python成员变量不会在具有相同优先级的不同线程中更新

在Python中使用readline函数时如何向下行

当值是一个integer时,在Python中使用JMESPath来验证字典中的值(例如:1)

更改Seaborn条形图中的x轴日期时间限制

阅读Polars Python中管道的函数定义

请从Python访问kivy子部件的功能需要帮助

从DataFrame.apply创建DataFrame

在Python和matlab中显示不同 colored颜色 的图像

优化在numpy数组中非零值周围创建缓冲区的函数的性能

从webhook中的短代码(而不是电话号码)接收Twilio消息

Python daskValue错误:无法识别的区块管理器dask -必须是以下之一:[]

Matlab中是否有Python的f-字符串等效物

. str.替换pandas.series的方法未按预期工作

如何更改分组条形图中条形图的 colored颜色 ?

如何创建一个缓冲区周围的一行与manim?

mypy无法推断类型参数.List和Iterable的区别

调用decorator返回原始函数的输出

Python Pandas获取层次路径直到顶层管理

OpenCV轮廓.很难找到给定图像的所需轮廓