Background: I have a small feature in my Tkinter application which randomizes two random values from list and prints them to GUI. But I'm struggling to get right image (team logo) with random value.
Goal: is to random team name (from list of 28 teams) and GUI should show team logo also. Pressing button "RANDOM" and it generates Team1 & Team2 and both should have logos next of the texts.
I have done research about: I had time with this answer, but I could not figure it out. This dice game kinda does it but I need two values instead of one and my list is full of strings.
Example: https://stackoverflow.com/a/27913052/5132305
import random
import tkinter as tk
from PIL import ImageTk, Image
# Use a class that hold all Team data
class Team():
def __init__(self, name, img_filename):
self.name = name
self.img_filename = img_filename
@property
def image(self):
# TODO: Open image file and return .PhotoImage object
team_logo = Image.open("%s.jpg" % (self.img_filename))
render = ImageTk.PhotoImage(team_logo)
self.img_filename.image = render
return self.img_filename
#This is how the class prints
def __str__(self):
return "Name: {} Image:{}".format(self.name, self.img_filename)
class MainWindow(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
root.geometry("700x450")
text_1 = tk.Label(text="VS", fg="red", anchor="center")
text_1.place(relx=.5, rely=.5, anchor="center")
text_2 = tk.Label(text="RANDOM GAME", fg="green", anchor="n")
text_2.pack(side="top")
# Button for randomize
self.parent = parent
self.frame = tk.Frame(self.parent)
self.randomButton = tk.Button(self.frame, text = 'RANDOM',
command=self.genRanTeam)
self.randomButton.pack(side="bottom")
self.frame.pack(side="bottom")
self.home_name = tk.StringVar()
self.visitor_name = tk.StringVar()
if 0: # TODO: Implement Team.image
# Images, I should somehow use this with home/visitor label?
# Yes, look at TODO in def genRanTeam(...
load = Image.open("%s.jpg" % (self.home.image))
render = ImageTk.PhotoImage(load)
# ONLY, if you decide to use OWN Label for Image
# What you suggest for this? So if I decide to only use home/visitor labels, I don't need next 4 lines of code (after this text?)
self.team_logo = tk.Label(parent, image=render)
self.team_logo.image = render
else:
self.team_logo = None
# Home team
self.home_label = tk.Label(Image = self.team_logo, textvariable=self.home_name)
self.home_label.pack(side="left")
# Visitor team
self.visitor_label = tk.Label(Image = self.team_logo, textvariable=self.visitor_name)
self.visitor_label.pack(side="right")
self.__init__Teams()
# Hold this in a own function for brevity
def __init__Teams(self):
self.teams = [Team('Chicago Fire', 'chicago'), Team('New York \
Red Bulls', 'newyork'), Team('Philadelphia Union', 'phila'),
Team('Toronto FC', 'toronto')]
# Init defaults
self.home = self.teams[0]
self.visitor = self.teams[0]
def genRanTeam(self):
# Use the initalized Teams from MainWindow..__init__
self.__init__Teams()
self.home = random.choice(self.teams)
self.visitor = None
# Loop while home == visitor
while self.visitor is None or self.home.name is self.visitor.name:
self.visitor = random.choice(self.teams)
self.home_name.set(self.home.name)
self.visitor_name.set(self.visitor.name)
# TODO: Configure .team_label with Team.image
# self.home_label.configure(image=self.home.image)
self.home_label.configure(image=self.home.image)
self.visitor_label.configure(image=self.visitor.image)
if __name__ == "__main__":
root = tk.Tk()
main = MainWindow(root)
main.pack(side="top", fill="both", expand=True)
root.mainloop()
Now when I am pressing RANDOM-button, it does random correctly values to variables x & y from list.
I also have 28 images in folder project/images. All of the images are "team_name.jpg"
Question 1: How do I loop list and match team name and logo?
All help is appreciated and some code review would be great also! (First question in SO!)