basically im trying to create a flag guessing game but im having problems with showing the images onto the certain levels. when someone clicks a certain level i want a random flag to show and the user to guess the name. but ive tried a few things and none work so far idk what im doing wrong or how to call the images.
the full code:
import tkinter as tk
import tkinter
import random
from tkinter import *
from tkinter import ttk, Canvas, NW
from PIL import ImageTk, Image
import os
window = tk.Tk()
window.title("Flags Guessing Game")
window.geometry("600x500")
bg = '#d9b9e2'
bg1 = '#b9e2df'
bg2 = '#e2b9d0'
bg3 = '#dbe2b9'
window.configure(background=bg)
def level_pick():
ebtn=tk.Button(window,font=('Times', 30), command=easy_level)
ebtn["bg"] = "#b6d6ff"
ebtn["justify"] = "center"
ebtn["text"] = "Easy"
ebtn.place(x=10,y=170,width=282,height=145)
mbtn=tk.Button(window,font=('Times', 30), command=medium_level)
mbtn["bg"] = "#f9ffa8"
mbtn["justify"] = "center"
mbtn["text"] = "Medium"
mbtn.place(x=310,y=170,width=277,height=145)
hbtn=tk.Button(window,font=('Times', 30), command=hard_level)
hbtn["bg"] = "#ffb3fc"
hbtn["justify"] = "center"
hbtn["text"] = "Hard"
hbtn.place(x=150,y=330,width=282,height=145)
diflbl=tk.Label(window,font=('Times', 20),bg=bg)
diflbl["text"] = "Choose a difficulty:"
diflbl["justify"] = "center"
diflbl.place(x=160,y=100)
titlelbl=tk.Label(window,font=('Times', 25),bg=bg)
titlelbl["text"] = "Flag Guessing Game"
titlelbl["justify"] = "center"
titlelbl.place(x=110,y=40)
def easy_level():
new = Toplevel(window)
new.title("Easy Mode")
new.configure(background=bg1)
new.geometry("600x500")
ans=tk.Entry(new,font=('Times', 25))
ans["justify"] = "center"
ans["borderwidth"] = "1px"
ans.place(x=90,y=270,width=414,height=188)
rules1=tk.Label(new, font=('Times', 20),bg=bg1)
rules1["text"] = "Enter Country Name Here:"
rules1["justify"] = "center"
rules1.place(x=110,y=230)
#easyimg = ["flags/canada.jpg","flags/spain.jpg","flags/mexico.jpg"]
# show random image
path="flags"
files=os.listdir(path)
d=random.choice(files)
os.system(d)
def medium_level():
new = Toplevel(window)
new.title("Medium Mode")
new.configure(background=bg2)
new.geometry("600x500")
ans=tk.Entry(new,font=('Times', 25))
ans["justify"] = "center"
ans["borderwidth"] = "1px"
ans.place(x=90,y=270,width=414,height=188)
rules2=tk.Label(new, font=('Times', 20),bg=bg2)
rules2["text"] = "Enter Country Name Here:"
rules2["justify"] = "center"
rules2.place(x=110,y=230)
def hard_level():
new = Toplevel(window)
new.title("Hard Mode")
new.configure(background=bg3)
new.geometry("600x500")
ans=tk.Entry(new,font=('Times', 25))
ans["justify"] = "center"
ans["borderwidth"] = "1px"
ans.place(x=90,y=270,width=414,height=188)
rules3=tk.Label(new, font=('Times', 20),bg=bg3)
rules3["text"] = "Enter Country Name Here:"
rules3["justify"] = "center"
rules3.place(x=110,y=230)
level_pick()
tk.mainloop()
this is one of the things i tried:
path="flags"
files=os.listdir(path)
d=random.choice(files)
os.system(d)
Here is a simple demonstration* showing the basics of getting and displaying a random image with tkinter
and Pillow
. For the example to run correctly, the .py file should be in a folder at the same level as another folder named "FlagPics." The "FlagPics" folder should contain 3 images: "FirstFlag.png," "SecondFlag.png," and "ThirdFlag.png." These images can be anything: just rename them as above, or change the image names in the code to match your images' actual names.
The list pics
contains references to the 3 images in the "FlagPics" folder, created using the Pillow
methods documented here: https://www.pythontutorial.net/tkinter/tkinter-photoimage/. The picture which is initially displayed when you run the .py file will be randomly chosen from the pics
list, and every time the user clicks the button, a new random picture will be selected from the same list.
Note that the image is displayed by the tkinter
Label
widget, since you set the Label
's image
property to an instance of ImageTk.PhotoImage
. Other tkinter
widgets can also display images - examples include the Button
and Canvas
widgets (related question here: How to insert an image in a canvas item?).
import tkinter as tk
from PIL import ImageTk, Image
from random import randint
window = tk.Tk()
window['width'] = 400
window['height'] = 400
pics = [ImageTk.PhotoImage(Image.open("FlagPics/FirstFlag.png")),
ImageTk.PhotoImage(Image.open("FlagPics/SecondFlag.png")),
ImageTk.PhotoImage(Image.open("FlagPics/ThirdFlag.png"))]
def rerandomize(label):
label['image'] = pics[randint(0, 2)]
flagLabel = tk.Label(master=window, image=pics[randint(0, 2)])
flagLabel.place(relx=0.25, rely=0.25, relheight=0.5, relwidth=0.5)
rerandomizeButton = tk.Button(window, text="Rerandomize", command = lambda: rerandomize(flagLabel))
rerandomizeButton.place(relx=0.35, rely=0.85, relheight=0.1, relwidth=0.3)
window.mainloop()
Now that you have your random images displayed, you may also be interested in how to resize them: https://www.geeksforgeeks.org/how-to-resize-image-in-python-tkinter/
*Full credit goes to user acw1668 for the idea - I only constructed a runnable example.