forked from flatplanet/Intro-To-TKinter-Youtube-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dice.py
78 lines (60 loc) · 1.66 KB
/
dice.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from tkinter import *
import random
root = Tk()
root.title('Codemy.com - Roll the Dice')
root.iconbitmap('c:/gui/codemy.ico')
root.geometry("500x500")
# Get The Dice Number
def get_number(x):
if x == '\u2680':
return(1)
elif x == '\u2681':
return(2)
elif x == '\u2682':
return(3)
elif x == '\u2683':
return(4)
elif x == '\u2684':
return(5)
elif x == '\u2685':
return(6)
# Roll the dice
def roll_dice():
# Roll Random Dice
d1 = random.choice(my_dice)
d2 = random.choice(my_dice)
# Determine Dice Number
sd1 = get_number(d1)
sd2 = get_number(d2)
# Update Labels
dice_label1.config(text=d1)
dice_label2.config(text=d2)
# Update Sub Labels
sub_dice_label1.config(text=sd1)
sub_dice_label2.config(text=sd2)
# Update Total Label
total = sd1 + sd2
total_label.config(text=f"You Rolled: {total}")
# Create A Dice List
my_dice = ['\u2680', '\u2681','\u2682','\u2683','\u2684','\u2685',]
# Create a Frame
my_frame = Frame(root)
my_frame.pack(pady=20)
# Create Dice Labels
dice_label1 = Label(my_frame, text='', font=("Helvetica", 100), fg="black")
dice_label1.grid(row=0, column=0, padx=5)
sub_dice_label1 = Label(my_frame, text="")
sub_dice_label1.grid(row=1, column=0)
dice_label2 = Label(my_frame, text='', font=("Helvetica", 100), fg="black")
dice_label2.grid(row=0, column=1, padx=5)
sub_dice_label2 = Label(my_frame, text="")
sub_dice_label2.grid(row=1, column=1)
# Create Roll Button
my_button = Button(root, text="Roll Dice", command=roll_dice, font=("Helvetica", 24))
my_button.pack(pady=20)
# Create Totals Label
total_label = Label(root, text="", font=("Helvetica", 24), fg="grey")
total_label.pack(pady=40)
# Roll the dice
roll_dice()
root.mainloop()