-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dice Simulator.py
45 lines (34 loc) · 1.24 KB
/
Dice Simulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import tkinter
from PIL import Image, ImageTk
import random
# toplevel widget which represents the main window of an application
root = tkinter.Tk()
root.geometry('400x400')
root.title('Roll the Dice')
# Adding label into the frame
l0 = tkinter.Label(root, text="")
l0.pack()
# adding label with different font and formatting
# images
dice = ['die1.png', 'die2.png', 'die3.png', 'die4.png', 'die5.png', 'die6.png']
# simulating the dice with random numbers between 0 to 6 and generating image
image1 = ImageTk.PhotoImage(Image.open(random.choice(dice)))
# construct a label widget for image
label1 = tkinter.Label(root, image=image1)
label1.image = image1
# packing a widget in the parent widget
label1.pack( expand=True)
# function activated by button
def rolling_dice():
image1 = ImageTk.PhotoImage(Image.open(random.choice(dice)))
# update image
label1.configure(image=image1)
# keep a reference
label1.image = image1
# adding button, and command will use rolling_dice function
button = tkinter.Button(root, text='Roll the Dice', fg='blue', command=rolling_dice)
# pack a widget in the parent widget
button.pack( expand=True)
# call the mainloop of Tk
# keeps window open
root.mainloop()