Generic selectors
Exact matches only
Search in title
Search in content
wb_sunny

GUI Dice Roll Simulation using Python

Certificate

Let’s create a dice roll simulation code using the Python tkinter library. We all love playing board games like snakes and ladders, ludo our utmost favorite one. But what if you don’t have a dice or you lost it somewhere. It’s a major mood off for everyone who is excited to play along. But what if you’ve got it covered with your python programming skills?


Using the Tkinter Library in Python for Drawing Dice

Python offers various packages to design the Graphical User Interface. Tkinter is the most popular, common, faster, and easy to use Python package used to build GUI applications.

It provides a powerful Object-Oriented Interface and is easy to use. Also, you develop an application; you can use it on any platform, which reduces the need of amendments required to use an app on Windows, Mac, or Linux.

This framework provides Python users with a simple way to create GUI elements using the widgets found in the Tk toolkit.

Tk widgets can be used to construct buttons, menus, data fields, etc. in a Python application.

For the dice roll simulation program to run successfully and for building up GUI, you must import the python tkinter library.

If its not already installed on your system using the pip package manager:

pip install tk

Coding the Dice Roll Simulation in Python

Here’s the complete code for the program. We’ll break down the individual sections below, to help you better understand the code.

#import the required libraries
#tkinter library to create GUI
#random library because we're randomly selecting numbers
from tkinter import *
import random

#create tkinter instance
root=Tk()
#define geometry
root.geometry("400x400")

#GUI will have two basic components
#1.Button which will trigger the rolling of dice
#2.Dice label
l1=Label(root,font=("Helvetica",260))

def roll():
    #create a number variable in which the list of all the ASCII characters of the string will be stored
    #Use backslash because unicode must have a backslash 
    dice=['\u2680','\u2681','\u2682','\u2683','\u2684','\u2685']
    #configure the label
    l1.config(text=f'{random.choice(dice)}{random.choice(dice)}')
    l1.pack()
    
b1=Button(root,text="Roll the Dice!",foreground='blue',command=roll)
b1.place(x=300,y=0)
b1.pack()

root.mainloop()

Understanding the Code

#import the required libraries
#tkinter library to create GUI
#random library because we're randomly selecting numbers
from tkinter import *
import random

Initially we import the necessary libraries in python. To get access to the random module, we add from random import * to the top of our program. from Tkinter import * imports every exposed object in Tkinter into your current namespace.

#create tkinter instance
root=Tk()
#define geometry
root.geometry("400x400")
  • root = Tk( ) : The root window is created. The root window is a main application window in our programs. It has a title bar and borders. These are provided by the window manager. It must be created before any other widgets.
  • root.geometry(“400×400”) : The geometry method sets a size for the window and positions it on the screen. The two parameters are the width and height of the window.
def roll():
    #unicodes must have a backslash
    dice=['\u2680','\u2681','\u2682','\u2683','\u2684','\u2685']
    l1.config(text=f'{random.choice(dice)}{random.choice(dice)}')
    l1.pack()

We will now define our function roll( ) to create our dice roll simulation. Now we will create a dice variable in which we will store the list of all the ASCII characters of that string will be stored.

We will then congif the label.

The choices() method returns a list with the randomly selected element from the specified sequence. The l1.pack( ) is a geometry manager that organizes widgets in blocks before placing them in the parent widget.

root.mainloop( ): Finally, we enter the mainloop. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. It is terminated when we click on the close button of the titlebar.

Result

dice roll simulation
dice roll simulation

Conclusion

We hope you enjoyed building the dice roll simulation app with us today. For more fun tutorials like these, continue to follow AskPython.