Python

Dice Rolling Simulator – Python

Dice is a cube-shaped object used in games like ludo, snakes and ladders, bunko and Yahtzee, etc. The cube when spun faces up with a random number ranging from 1-to 6.

A dice rolling simulator is a digital computer model that works like normal dice and shows a random number. It is developed using a software program that can be coded in any programming language.

In this article, we will be coding a dice-rolling simulator using python language with GUI using the Tkinter library.

We will start by importing the modules and then proceed with the remaining part.

How To Create A Dice-Rolling Simulator Using Python?

The only module we will be requiring for this program will be Tkinter. Let’s start by importing the libraries first.

Importing the libraries

We will first import Tkinter for the GUI and then RANDOM as we are going to display the random number. Now we must create an instance for Tkinter and define the geometry (Height and width) for our resultant interface.

The Code Block:

Defining the Function

Define the function roll and write the numbers on dice by creating a variable “number”. Store the ASCII values for all the numbers on the dice. Configure the label using “Config” and format the text accordingly. Refer to the code block below. Finally, Pack the label l1.

The Code Block:

Working With GUI Components

The GUI will have two components. The first one is a button to give the command for rolling and the second one is the dice label itself. Starting with the label part, we will have GUI as an instance comprising fonts and font size. Create the button B1 and assign command=Roll. Place the button at half of the x-Axis and top of the display, keeping y-axis to the zero.

The Code Block:

Once you save your code and RUN it, you will see a button to roll the dice.

The Complete Code For Dice Rolling Simulator:

Now, let’s have some insights on other aspects of Tkinter and its alternatives in Python.

What Is Tkinter in Python?

Tkinter is a framework used to create Graphical user interfaces in Python. It is the only GUI framework that is built into the python standard library and is included in all the python standard distributions. Tkinter provides a simple method to create the interface elements from the TK toolkit. These widgets can be used to create buttons, tables, data fields, etc.

Tkinter has been widely used as a GUI framework but there are some cool alternatives that you can use instead of Tkinter. Here are a few of them:

PyQT

PyQT is a python binding for QT, which is a cross platform toolkit for GUI. It is a free software developed by Riverbank Computing and is implemented in python as a plugin. PyQT is used by some of the leading applications such as Anki, Calibre, Dropbox, Openshot and many more.

Kivy

When compared to other GUI frameworks including tkinter, Kivy is most preferred. Kivy can be used to create android apps and also helps in maintaining the robustness of the software. You can use the same project to host apps on iOS, Android, Windows and OSx. Since, majority of app developers use Java as their main language, Kivy is not so popular. Also, Kivy doesn’t support python 3.8.

Pyglet

Pyglet is a library for python that has an OOP interface. You can create games and multimedia applications using Pyglet.  You can also try full-screened operations and multi monitors setups while using Pyglet.

The Conclusion

Dice rolling simulator is not tough code to write and can be completed in 10-15 lines only. This is what happens when you code in python. We also discussed some Tkinter alternatives that you can use as a GUI framework. The Dice Rolling Simulator is an easy coding project for beginners in python. I hope you loved reading this article.

About the author

Shalini Salotra