每当我在函数内的标签小部件中按下按钮时,我想显示图像

问题描述 投票:0回答:1
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() 函数显示图像,但它只显示一个空标签。我期待每当我按下添加卡按钮时,第一帧就会弹出一个图像,但不幸的是没有显示图像。

python python-imaging-library
1个回答
0
投票

我希望每当我按下添加卡按钮时,都会出现一个 图像在第一帧弹出,但没有图像显示 不幸的是。

问题可以解决。

将其添加到第 21 行:

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

截图:

© www.soinside.com 2019 - 2024. All rights reserved.