GUI Dice Roll Simulation using 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.
- We must have basic knowledge of Python, Tkinter, and Random module.
- Tkinter module should be installed on the system.
Tkinter can be installed by running the following command in the command prompt:
pip install tk
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”.
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.
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”.