from tkinter import * import random class Dice: def __init__(self, f): self.pictures = list() # Laddar in alla bilder till en lista. for i in range(1,7): filnamn = str(i)+".gif" self.pictures.append(PhotoImage(file=filnamn)) self.utfall = 1 self.bild = self.pictures[self.utfall-1] self.label = Label(f, image = self.bild) self.label.pack(side = LEFT) # Tärningarna ska ligga vänster->höger och inte upp->ner som är standard för pack def kasta(self): self.utfall = random.randint(1,6) # Byter till "rätt" bild i listan. self.bild = self.pictures[self.utfall-1] self.label.configure(image=self.bild) class Spel: def kasta(self): for i in range(5): self.dices[i].kasta() def __init__(self, root): root.geometry('240x120') root.title('Tärning') f = Frame(root) f.pack() self.dices = list() for i in range(5): self.dices.append(Dice(f)) self.button = Button(root, text="Kasta", fg="red", command=self.kasta) self.button.pack() root = Tk() Spel(root) root.mainloop()