|
1 | | -### In this script we will create a classic rolling dice simulator with the help of basic Python knowledge. Here we will be using the random module since we randomize the dice simulator for random outputs. |
| 1 | +#Dice Rolling Simulator |
2 | 2 |
|
3 | | -### random.randint(): This function generates a random number in the given range. |
| 3 | +## Description |
| 4 | +We all know about dice. It’s a simple cube with numbers from 1 to 6 written on its face. But what is simulation? It is making a computer model. Thus, a dice simulator is a simple computer model that can roll a dice for us. |
4 | 5 |
|
5 | | -### Below is the implementation. |
6 | | -[Implementation](https://github.com/Ayush7614/Amazing-Python-Scripts/blob/master/Dice%20Roll%20Simulator/program.py) |
| 6 | +</br> |
7 | 7 |
|
| 8 | +### Step 1: Importing the required modules |
| 9 | +</br> |
| 10 | + |
| 11 | +We will import the following modules: |
| 12 | +- Tkinter: Imported to use Tkinter and make GUI applications. |
| 13 | +- Image, Imagetk: Imported from PIL, i.e. Python Imaging Library. We use it to perform operations involving images in our UI. |
| 14 | +- Random: Imported to generate random numbers. |
| 15 | + |
| 16 | +</br> |
| 17 | + |
| 18 | +### Step 2: Building a top-level widget to make the main window for our application |
| 19 | +</br> |
| 20 | + |
| 21 | +In this step, we will build the main window of our application, where the buttons, labels, and images will reside. We also give it a title by title() function. |
| 22 | + |
| 23 | +</br> |
| 24 | + |
| 25 | +### Step 3: Designing the buttons |
| 26 | +</br> |
| 27 | + |
| 28 | +Now, just think, what we need to roll a die? Just our hands! |
| 29 | + |
| 30 | +Here, we use **pack()** to arrange our widgets in row and column form. The ‘BlankLine’ label is to skip a line, whereas we use ‘HeadingLabel’ label to give a heading. |
| 31 | + |
| 32 | +- **root** – the name by which we refer to the main window of the application |
| 33 | +- **text** – text to be displayed in the HeadingLabel |
| 34 | +- **fg** – the colour of the font used in HeadingLabel |
| 35 | +- **bg** – background colour of the HeadingLabel |
| 36 | +- **font** – used to give customised fonts to the HeadingLabel text |
| 37 | +- **.pack()** – Used to pack the widget onto the root window |
| 38 | + |
| 39 | +</br> |
| 40 | + |
| 41 | +### Step 4: Forming a list of images to be randomly displayed |
| 42 | +</br> |
| 43 | + |
| 44 | +‘dice’ is the list of names of images kept in same folder, which are chosen randomly according to the random number generated. |
| 45 | +</br> |
| 46 | + |
| 47 | +‘DiceImage’ is used to store an image of dice which is chosen by randomly generated numbers. |
| 48 | + |
| 49 | +</br> |
| 50 | + |
| 51 | +### Step 5: Constructing a label for image, adding a button and assigning functionality |
| 52 | +</br> |
| 53 | + |
| 54 | +‘ImageLabel’ is to place an image in the window. The parameter expands declared as True so that even if we resize the window, image remains in the center. |
| 55 | +</br> |
| 56 | + |
| 57 | +Major function: |
| 58 | + |
| 59 | +‘rolling_dice’ function is a function that is executed every time a button is clicked. This is attained through the ‘command=rolling_dice’ parameter while defining a button. |
| 60 | + |
| 61 | +</br> |
| 62 | + |
| 63 | +### Step 6: Forming a list of images to be randomly displayed |
| 64 | +</br> |
| 65 | + |
| 66 | +‘root.mainloop()’ is used to open the main window. It acts as the main function of our program. |
0 commit comments