Open In App

GUI Dice Roll Simulation using Python

Last Updated : 02 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Importing tkinter and random modules
from tkinter import *
import random
  
# Creating a window
window = Tk()
  
# Setting background colour of the window
window.configure(bg="black")
  
# Setting geometry of the window
window.geometry("550x350")
  
# Setting title of the window
window.title("Rolling The Dices Game")
  
# Preventing maximizing of the window
window.resizable(0, 0)
  
window.mainloop()


Output:

Rolling The Dices Game using Tkinter and Random in Python.

 

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




# Importing tkinter and random modules
from tkinter import *
import random
  
# Creating a window
window = Tk()
  
# Setting background colour of the window
window.configure(bg="black")
  
# Setting geometry of the window
window.geometry("550x350")
  
# Setting title of the window
window.title("Rolling The Dices Game")
  
# Preventing maximizing of the window
window.resizable(0, 0)
  
# Adding button to roll the dices
roll_button = Button(window, text="Roll!",
                     width=10, height=2,
                     font=15, bg="aqua", bd=2)
# Setting the position of the button
roll_button.pack(padx=10, pady=15)
  
# Adding Label
label = Label(window, font=("times", 250),
              bg="black", fg="yellow")
  
window.mainloop()


Output:

Rolling The Dices Game using Tkinter and Random in Python.

 

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




# Importing tkinter and random modules
from tkinter import *
import random
  
# Creating a window
window = Tk()
  
# Setting background colour of the window
window.configure(bg="black")
  
# Setting geometry of the window
window.geometry("550x350")
  
# Setting title of the window
window.title("Rolling The Dices Game")
  
# Preventing maximizing of the window
window.resizable(0, 0)
  
# Creating function to roll the dices
def roll_dices():
     # These values indicate dots on the dices.
     # For eg: \u2680 corresponds to 1 dot,
     # \u2681 corresponds to 2 dots etc.
    dice_dots = ['\u2680', '\u2681',
                 '\u2682', '\u2683',
                 '\u2684', '\u2685']
    # Generating random dots on the dices
    label.configure(
        text=f'{random.choice(dice_dots)}{random.choice(dice_dots)}')
    label.pack()
  
  
# Adding button to roll the dices
roll_button = Button(window, text="Roll!",
                     width=10, height=2,
                     font=15, bg="aqua",
                     bd=2, command=roll_dices)
# Setting the position of the button
roll_button.pack(padx=10, pady=15)  
  
# Adding Label
label = Label(window, font=("times", 250),
              bg="black", fg="yellow")
  
window.mainloop()


Output:

Rolling The Dices Game using Tkinter and Random in Python.

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads