Python Forum
Dice Throwing Game in Python (I have a problem)
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dice Throwing Game in Python (I have a problem)
#1
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()
Reply
#2
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
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  json loads throwing error mpsameer 8 678 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  Stacking Problem in a game. HelloTobi22 2 992 Aug-05-2022, 09:48 AM
Last Post: HelloTobi22
  While loop not ending (Best of 10 dice game) K3nidi 3 1,487 Jul-09-2022, 09:53 AM
Last Post: K3nidi
  Problem with my pong game code Than999 8 3,839 May-15-2022, 06:40 AM
Last Post: deanhystad
  pywin32 Illustrator Throwing Exception Error matthewsjc1 7 5,556 Aug-27-2021, 02:43 AM
Last Post: Larz60+
  Problem restricting user input in my rock paper scissors game ashergreen 6 4,590 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  Guessing game problem IcodeUser8 7 3,618 Jul-19-2020, 07:37 PM
Last Post: IcodeUser8
  PyAudio throwing Input overflowed anthares 3 4,762 Jun-14-2020, 03:37 PM
Last Post: anthares
  Python Hangman Game - Multiple Letters Problem t0rn 4 4,643 Jun-05-2020, 11:27 AM
Last Post: t0rn
  game of the goose - dice problem koop 4 3,470 Apr-11-2020, 02:48 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020