GUI Dice Roll Simulation using Python
Last Updated :
02 Aug, 2022
In this article, we are going to create Rolling The Dices Game using Tkinter and a random module in Python.
A random module in Python is a built-in module used to generate random numbers for various distributions. Here, we will use this module to create random outcomes for our dice. Python offers various libraries to put our needs at ease and Tkinter is one of them. Tkinter is a standard library of Python with the help of which we can create Graphical User Interface applications easily and add widgets like buttons, scrollbars, text boxes, and labels to them.
Prerequisites
- We must have basic knowledge of Python, Tkinter, and Random module.
- Tkinter module should be installed on the system.
Installation
Tkinter can be installed by running the following command in the command prompt:
pip install tk
Stepwise Implementation
Now, we will write the Python script for our Rolling The Dices Game.
Step 1: Creating a simple Tkinter window.
In this step, we are simply creating a window and set the background color black with the window size “550*350” and set the title of the window as “Rolling The Dices Game”.
Python3
from tkinter import *
import random
window = Tk()
window.configure(bg = "black" )
window.geometry( "550x350" )
window.title( "Rolling The Dices Game" )
window.resizable( 0 , 0 )
window.mainloop()
|
Output:
Step 2: Add the Roll button and label it on the window.
In this step, we are going to add a button “Roll” in the window that we have created in the first step. When we click on the roll button the dices are rolled (randomly shuffled) and for that we have to create a function for the roll button. Let’s create that function in the next step.
Python3
from tkinter import *
import random
window = Tk()
window.configure(bg = "black" )
window.geometry( "550x350" )
window.title( "Rolling The Dices Game" )
window.resizable( 0 , 0 )
roll_button = Button(window, text = "Roll!" ,
width = 10 , height = 2 ,
font = 15 , bg = "aqua" , bd = 2 )
roll_button.pack(padx = 10 , pady = 15 )
label = Label(window, font = ( "times" , 250 ),
bg = "black" , fg = "yellow" )
window.mainloop()
|
Output:
Step 3: Creating a function to roll the dice.
In this step, we are going to make the function that can randomly select the numbers on the top of the dice using random.choice() function of random module for the roll button that we made in the previous step. Whenever we click on roll button random number of dots can be seen on the top of dice. To display the dots of dice on the top we are using Unicode characters of die faces such as “\u2680 for 1 dot” and “\u2681 for 2 dots”.
Python3
from tkinter import *
import random
window = Tk()
window.configure(bg = "black" )
window.geometry( "550x350" )
window.title( "Rolling The Dices Game" )
window.resizable( 0 , 0 )
def roll_dices():
dice_dots = [ '\u2680' , '\u2681' ,
'\u2682' , '\u2683' ,
'\u2684' , '\u2685' ]
label.configure(
text = f '{random.choice(dice_dots)}{random.choice(dice_dots)}' )
label.pack()
roll_button = Button(window, text = "Roll!" ,
width = 10 , height = 2 ,
font = 15 , bg = "aqua" ,
bd = 2 , command = roll_dices)
roll_button.pack(padx = 10 , pady = 15 )
label = Label(window, font = ( "times" , 250 ),
bg = "black" , fg = "yellow" )
window.mainloop()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...