'How to produce double images using Tkinter?
I am quite new to python and tkinter and this is my code, I am trying to create a spin machine game and I have gotten stuck on this bit, it produces the correct amount of images that I want it to (three), when there are only single times its come up, but when my code that decides which image should be put up decides two show two of the same image the code glitches up and only shows 1 out of the two no matter what I have tried, I tried assigning it to a list then showing it again but that didn't work as I dont think I did it correctly. Any help regarding this issue would be greatly appreciated, and an explanation would go a long way. BTW I have attached all image files to the pycharm project.
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
import random
global COLUMN
COLUMN = 0
def click1():
SpinSigns = ["Banana", "Bell", "Cherry", "Dollar", "Skull"]
BananaCounter = 0
BellCounter = 0
CherryCounter = 0
DollarCounter = 0
SkullCounter = 0
global COLUMN
COLUMN = 0
for i in range(3):
doubles = []
randomSign = SpinSigns[random.randint(0,4)]
if randomSign == "Banana":
global banana
banana = ImageTk.PhotoImage(Image.open("BananaSign.png"))
Banana = Label(window, image=banana)
Banana.grid(row=0, column= COLUMN)
BananaCounter += 1
print(randomSign)
if BananaCounter == 2:
doubles.append(Banana)
doubles[0].grid(row = 0, column = 4)
elif randomSign == "Bell":
global bell
bell = ImageTk.PhotoImage(Image.open("BellSign.png"))
Bell = Label(window, image=bell)
Bell.grid(row=0, column= COLUMN)
BellCounter += 1
print(randomSign)
if BellCounter == 2:
doubles.append(Bell)
doubles[0].grid(row = 0, column = 4)
elif randomSign == "Cherry":
global cherry
cherry = ImageTk.PhotoImage(Image.open("CherrySign.png"))
Cherry = Label(window, image=cherry)
Cherry.grid(row=0, column= COLUMN)
print(randomSign)
CherryCounter += 1
if CherryCounter == 2:
doubles.append(Cherry)
doubles[0].grid(row = 0, column = 4)
elif randomSign == "Dollar":
global dollar
dollar = ImageTk.PhotoImage(Image.open("DollarSign.png"))
Dollar = Label(window, image=dollar)
Dollar.grid(row=0, column= COLUMN)
DollarCounter += 1
print(randomSign)
if DollarCounter == 2:
doubles.append(Dollar)
doubles[0].grid(row = 0, column = 4)
elif randomSign == "Skull":
global skull
skull = ImageTk.PhotoImage(Image.open("SkullSign.png"))
Skull = Label(window, image=skull)
Skull.grid(row=0, column= COLUMN)
SkullCounter += 1
print(randomSign)
if SkullCounter == 2:
doubles.append(Skull)
doubles[0].grid(row = 0, column = 4)
COLUMN += 1
window = Tk()
window.geometry("600x450")
lever = Button(window, text="Pull Me!", command = click1)
# coins = Label(text=coinAmount)
# coins.grid(row = 0, column = 0)
#label1.grid(row = 0, column = 0)
lever.grid(row=1, column= 1)
window.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
