Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e7eec08

Browse files
Merge pull request avinashkranjan#517 from mehabhalodiya/DRS
Added Dice roll simulator
2 parents 578f264 + 29893bd commit e7eec08

File tree

9 files changed

+112
-54
lines changed

9 files changed

+112
-54
lines changed

‎Dice-Roll-Simulator/README.md‎

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,66 @@
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
22

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.
45

5-
### Below is the implementation.
6-
[Implementation](https://github.com/Ayush7614/Amazing-Python-Scripts/blob/master/Dice%20Roll%20Simulator/program.py)
6+
</br>
77

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.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import tkinter
2+
from PIL import Image, ImageTk
3+
import random
4+
5+
# toplevel widget which represents the main window of an application
6+
root = tkinter.Tk()
7+
root.geometry('400x400')
8+
root.title('Roll the Dice')
9+
10+
# Adding label into the frame
11+
l0 = tkinter.Label(root, text="")
12+
l0.pack()
13+
14+
# adding label with different font and formatting
15+
l1 = tkinter.Label(root, text="Dice Rolling Simulator", fg = "white",
16+
bg = "black",
17+
font = "Helvetica 16 bold italic")
18+
l1.pack()
19+
20+
# images
21+
dice = ['./Dice-Roll-Simulator/imgs/die1.png', './Dice-Roll-Simulator/imgs/die2.png', './Dice-Roll-Simulator/imgs/die3.png', './Dice-Roll-Simulator/imgs/die4.png', './Dice-Roll-Simulator/imgs/die5.png', './Dice-Roll-Simulator/imgs/die6.png']
22+
# simulating the dice with random numbers between 0 to 6 and generating image
23+
image1 = ImageTk.PhotoImage(Image.open(random.choice(dice)))
24+
25+
# construct a label widget for image
26+
label1 = tkinter.Label(root, image=image1)
27+
label1.image = image1
28+
29+
# packing a widget in the parent widget
30+
label1.pack( expand=True)
31+
32+
# function activated by button
33+
def rolling_dice():
34+
image1 = ImageTk.PhotoImage(Image.open(random.choice(dice)))
35+
# update image
36+
label1.configure(image=image1)
37+
# keep a reference
38+
label1.image = image1
39+
40+
41+
# adding button, and command will use rolling_dice function
42+
button = tkinter.Button(root, text='Click here to Roll the Dice', fg='blue', command=rolling_dice)
43+
44+
# pack a widget in the parent widget
45+
button.pack( expand=True)
46+
47+
# call the mainloop of Tk
48+
# keeps window open
49+
root.mainloop()

‎Dice-Roll-Simulator/imgs/die1.png‎

4.48 KB
Loading[フレーム]

‎Dice-Roll-Simulator/imgs/die2.png‎

4.91 KB
Loading[フレーム]

‎Dice-Roll-Simulator/imgs/die3.png‎

6.05 KB
Loading[フレーム]

‎Dice-Roll-Simulator/imgs/die4.png‎

5.26 KB
Loading[フレーム]

‎Dice-Roll-Simulator/imgs/die5.png‎

6.51 KB
Loading[フレーム]

‎Dice-Roll-Simulator/imgs/die6.png‎

5.43 KB
Loading[フレーム]

‎Dice-Roll-Simulator/program.py‎

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /