Simple Roulette
Simple Roulette
i tkinter
as
tk
import
random
import
time
import
math
import
tkinter.messagebox
as
messagebox
class
Shop:
def
__init__
(
self
, root):
self
.root = root
self
.root.title(
"Shop"
)
#title
self
.items = [
f"Item
{
i +1
}
"
fori
in
range
(
8
)
]
# range ng items 1 - 8
self
.prices = [
500
,
550,
570,
400
,
600
,
650
,
620
,
520
]
# sample prices
self
.voucher =
None
#declare muna as no voucher
self
.item_quantities = [
0
] *
len
(
self
.items)
#declare pang track ng
quantity
elf
s .item_labels = []
# labels items
self
.item_buttons = []
# buttons
self
.quantity_labels = []
#quantity labels
for
i, (item, price)
in
enumerate(
zip
(
self
.items,
self
.prices)):
label = tk.Label(
self
.root,
text
=
f"
{
item
}
:
${
p
rice
}
"
,
font
=(
"Arial"
,
14
))
# new label widget
label.pack()
self
.item_labels.append(label)
utton = tk.Button(
b self
.root,
text
=
"Buy"
,
command
=
lambda
i=i:
self
.buy_item(i))
button.pack()
# buttons for buying items
self
.item_buttons.append(button)
quantity_label = tk.Label(
self
.root,
text
=
"
Quantity:
0"
,
font
=(
"Arial"
,
12
))
quantity_label.pack()
# label for quantity
self
.quantity_labels.append(quantity_label)
self
.voucher_button = tk.Button(
self
.root,
text
=
"Get Voucher from
Roulette"
,
command
=
s
elf
.get_voucher)
self
.voucher_button.pack(
pady
=
10
)
# button
for getting voucher by
roulette
self
.checkout_button = tk.Button(
self
.root,
text
=
"Checkout"
,
command
=
s
elf
.checkout)
self
.checkout_button.pack(
pady
=
10
)
# checkout
button
def
buy_item
(
self
, index):
self
.item_quantities[index] +=
1
# sa quantities
plus 1 if click
self
.quantity_labels[index].config(
text
=
f"Quantity:
{
s
elf
.item_quantities[index]
}
"
)
def
get_voucher
(
self
):
self
.roulette_window = tk.Toplevel(
self
.root)
self
.roulette = Roulette(
self
.roulette_window)
# getting voucher frim
roullete in another window
self
.roulette.on_voucher_win =
self
.apply_voucher
def
apply_voucher(
self
, voucher):
self
.voucher = voucher
# to apply voucher in
a item
tk.messagebox.showinfo(
"Voucher Obtained"
,
f"You received a voucher:
{
v
oucher[
1
]
}
"
)
def
checkout
(
self
):
total =
0
checkout_details = []
# List to hold details
of items purchased
for
i
in
range
(
len
(
self
.items)):
price =
self.prices[i]
quantity =
self.item_quantities[i]
if
quantity >
0
:
# Only include items
that were purchased
item_total = price * quantity
# multiplythe price to how many
items purchased
total += item_total
# total of items
bought
checkout_details.append(
f"
{
self
.items[i]
}
(Quantity:
{
q
uantity
}
) - $
{
item_total
}
"
)
#details nung mga binili
class
Roulette:
def
__init__
(
self
, root):
self
.root = root
self
.root.title(
"Roulette"
)
#title of roulette
spin button
#
self
.spin_button = tk.Button(
self
.root,
text
=
"Spin"
,
command
=
self
.spin)
self
.spin_button.pack()
discount na makukuha
#
self
.items = [
$10 off item 1"
" ,
"$10 off item
2",
"$10 off item 3",
"$10 off item 4"
,
"$10 off item5"
,
"$10 off item 6",
"$10 off item 7"
,
"$10 off item8"
]
elf
s .countdown =
86400
# Set initial countdown
to 1 minute
self
.timer_running =
False
self
.spun_once =
False
# Flag to track if
spin has been used
def
spin
(
self
):
if not
self.spun_once
or
self
.countdown ==
0
:
# condition if kapag
zero na timer
self
.spin_button.config(
state
=
"disabled"
)
# naka disable kapag
nagrurun pa timer
self
.spin_anim()
self
.start_timer()
self
.spun_once =
True
# pwede na ulit magspinif zero na timer
ef
d spin_anim
(
self
):
# Randomly select the winning number from 1 - 8
winning_number = random.choice(
self
.numbers)
if
hasattr
(
self
,
'on_voucher_win'
):
self
.on_voucher_win((number,
self
.items[i]))
# Send the
number and item, pagapply ng voucher sa item 1 time lang
break
elf
s .timer_label.config(
text
=
f"
{
hours
:
02d
}
:
{
minutes
:
02d
}
:
{
seconds
:
02d
}
"
)
#label nung timer
def
show_winnings_details
(
s
elf
, event):
Display detailed winnings information in a popup window
#
popup = tk.Toplevel(
self
.root)
popup.title(
"Winnings Details"
)
popup.geometry(
"300x100"
)
def
start_timer
(
self
):
self
.timer_running =
True
# pagstart ng timer
self
.update_timer(
self
.root)
def
update_timer
(
self
, root):
if
self
.timer_running:
if
self
.countdown >
0
:
self
.countdown -=
1
# reverse timer
papunta zero
minutes, seconds =
divmod
(
self
.countdown,
60
)
# Ginamit divmod
function para maconvert ang remaining seconds into hours, minutes, and seconds
hours, minutes =
divmod(minutes,
60
)
# ang divmod ay ginamit
para sa pagcalculate ng total countdown papunta zero.
elf
s .timer_label.config(
text
=
f"
{
hours
:
02d
}
:
{
minutes
:
02d
}
:
{
seconds
:
02d
}
"
)
#
digits na display para sa countdown
root.after(
1000
,
self
.update_timer,
root)# ginamit pang update
ng timer hanggat hindi zero ito
else
:
self
.timer_running =
False # Stop
the timer when it reaches
zero
self
.spin_button.config(
state
=
"
normal"
)
# Enable the spin
button
self
.winnings_label.grid_forget()
# Hide the winnings label
if
__name__ ==
"__main__"
:
root = tk.Tk()
shop = Shop(root)
root.mainloop()