Two random values from list needs to match with images
Comment: Implement Team.image
Running your Team.image
gives me:
self.img_filename.image = renderAttributeError: 'str' object has no attribute 'image'
You can't add a new attribute to buildin str
object.This will work, change to:
self.render = ImageTk.PhotoImage(team_logo)return self.render
Comment: Do I need to make another tk.Label for team image?
There is no need to do so, this depends on your desired layout.
Start with one tk.Label(self, image=image, textvariable=name)
This looks like:
Relevant: label-on-top-of-image-in-python
Comment: Is it possible that self.visitor can show team+logo?
It's not the function of class Team()
to show anything, it's the job of tk.Lable(...
.
Relevant: updating-tkinter-label-with-an-image
Question: Two random values from list needs to match with images
This approach don't use two lists, it defines both values(team name, team image) in one class. Therefore no matching is needed.
For example:
# Use a class that hold all Team dataclass Team(): def __init__(self, name, img_filename): self.name = name self.img_filename = 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): # Up to 28 teams - Defined in __init__ only once # Create a list with all Teams using class Team self.teams = [Team('Chicago Fire', 'logo1.jpg'), Team('New York Red Bulls', 'logo2.jpg'), Team('Philadelphia Union', 'logo3.jpg'), Team('Toronto FC', 'logo4')] # Init defaults self.home = self.teams[0] self.visitor = self.teams[0] def genRanTeam(self): # Use the initalized Teams from __init__ self.home = random.choice(self.teams) self. visitor = None # Loop while home == visitor while self.visitor is None or self.home is self.visitor: self.visitor = random.choice(self.teams)if __name__ == "__main__": import time root = tk.Tk() main = MainWindow(root) for _ in range(3): main.genRanTeam() print("home:{} :\tvisitor:{}".format(main.home, main.visitor)) time.sleep(1)
Output:
home:Name: Toronto FC Image:logo4 : visitor:Name: Chicago Fire Image:logo1.jpghome:Name: New York Red Bulls Image:logo2.jpg : visitor:Name: Toronto FC Image:logo4home:Name: Toronto FC Image:logo4 : visitor:Name: New York Red Bulls Image:logo2.jpg
Tested with Python: 3.4.2