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()
I am trying to display an image using the addCard() function , but it just display an empty label. I am expecting whenever I pressed the add card button , there is an image pop out at the first frame , but no image was display unfortunately.
I am expecting whenever I pressed the add card button , there is an image pop out at the first frame , but no image was display unfortunately.
The problem can be fixed. On line 20: Change this:
cards.place(x = 20 , y=50)
to:
cards.place(x = 2, y=20
Add this on line 21:
def addCards():
.
.
.
cards.place(x = 2, y=20)
cards.image = imgTest #<== Add this.
Screenshot: