Python Forum

Full Version: Dice Throwing Game in Python (I have a problem)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I tried to make a program which says: "Press the button to throw the dices" and when you press the button it shows 2 random dice images. But it does not show pics eventhough the form application gets bigger when I press the button which means it takes pictures but don't show them (I think) Does anyone know how to fix this problem?

Also when I click the button for second time I want delete old images and create new two random dice images. How can I do that?

Thanks for all your help :)

from tkinter import *
import random

root = Tk()

topframe = Frame(root)
topframe.pack(side=TOP)
bottomframe = Frame(root)
bottomframe.pack(side=BOTTOM)

def selecter():
   randomNumber = random.randrange(1,666)
   if randomNumber < 111:
       zar1 = PhotoImage(file=r'C:\Users\aliyu_000\Desktop\zarlar\1.png')
       zar1label = Label(bottomframe, image=zar1)
       zar1label.pack(side=LEFT)
   elif randomNumber < 222:
       zar2 = PhotoImage(file=r'C:\Users\aliyu_000\Desktop\zarlar\2.png')
       zar2label = Label(bottomframe, image=zar2)
       zar2label.pack(side=LEFT)
   elif randomNumber < 333:
       zar3 = PhotoImage(file=r'C:\Users\aliyu_000\Desktop\zarlar\3.png')
       zar3label = Label(bottomframe, image=zar3)
       zar3label.pack(side=LEFT)
   elif randomNumber < 444:
       zar4 = PhotoImage(file=r'C:\Users\aliyu_000\Desktop\zarlar\4.png')
       zar4label = Label(bottomframe, image=zar4)
       zar4label.pack(side=LEFT)
   elif randomNumber < 555:
       zar5 = PhotoImage(file=r'C:\Users\aliyu_000\Desktop\zarlar\5.png')
       zar5label = Label(bottomframe, image=zar5)
       zar5label.pack(side=LEFT)
   else:
       zar6 = PhotoImage(file=r'C:\Users\aliyu_000\Desktop\zarlar\6.png')
       zar6label = Label(bottomframe, image=zar6)
       zar6label.pack(side=LEFT)

def selecter2x():
   selecter()
   selecter()

infozarat = Label(topframe, text="Zar atmak için butona basın:")
infozarat.pack(side=LEFT)

zarat = Button(topframe, text=' Zar AT ', bg="black", fg="white", command=selecter2x)
zarat.pack(side=LEFT)

root.mainloop()
for us to redo your program and get the same output to see what the problem is we would need your images. Which also leads me to why are you using 6 images instead of a spritesheet? You should have already loaded all the images into memory before hand. In that way you are not loading the images everytime you run selector(). Though this is done in pygame the tutorial on that specific task can be shown in the section of the dice spritesheet https://python-forum.io/Thread-PyGame-Lo...ets-part-2