In this tutorial, we'll create a simple Dice Game where you can play against the computer. We'll use Tkinter for the GUI and Pythonโs random module for dice rolls.
Step 1: Import Libraries
We need tkinter for the GUI, messagebox for pop-up dialogs, and random for generating dice rolls.
import tkinter as tk
from tkinter import messagebox
import random
Explanation:
tkinter โ main GUI library in Python
messagebox โ to show dialogs like "About"
random โ to simulate dice rolls
Step 2: Define the Theme
Let's set some colors for the app to make it look modern and consistent.
# App Colors
APP_BG = "#121212"
PANEL_BG = "#1F1F1F"
BTN_BG = "#2C2C2C"
BTN_HOVER = "#3A3A3A"
BTN_ACTIVE = "#FF6F61"
ACCENT = "#FF6F61"
TEXT_CLR = "#E0E0E0"
SUBTEXT_CLR = "#AAAAAA"
INPUT_BG = "#333333"
INPUT_FG = "#FFFFFF"
Explanation:
These colors will be used for backgrounds, buttons, text, and highlights.
Using constants makes it easy to tweak the theme later.
Step 3: Create the Main App Class
We'll encapsulate our game in a class called DiceGameApp.
class DiceGameApp:
def __init__(self, root):
self.root = root
root.title("MateTools โ Dice Game")
root.geometry("1000x500")
root.configure(bg=APP_BG)
root.resizable(False, False)
Explanation:
root.title() โ sets the window title
root.geometry() โ sets window size
root.configure(bg=APP_BG) โ sets background color
root.resizable(False, False) โ prevents resizing
Step 4: Create the Left Panel (Actions)
This panel contains buttons like Roll Dice, Clear History, and About.
# LEFT PANEL
left = tk.Frame(root, bg=PANEL_BG, width=420)
left.pack(side="left", fill="y")
tk.Label(left, text="MateTools", bg=PANEL_BG, fg=ACCENT, font=("Segoe UI", 20, "bold")).pack(padx=16, pady=16)
tk.Label(left, text="Dice Game", bg=PANEL_BG, fg=TEXT_CLR, font=("Segoe UI", 14, "bold")).pack(anchor="w", padx=16)
tk.Label(left, text="Play against the computer", bg=PANEL_BG, fg=SUBTEXT_CLR, font=("Segoe UI", 10)).pack(anchor="w", padx=16)
Explanation:
Frame โ a container for other widgets
Label โ shows text like headers or instructions
pack() โ positions elements inside the frame
Step 5: Create Buttons with Hover Effects
We can define a helper function to make consistent buttons.
btn_frame = tk.Frame(left, bg=PANEL_BG)
btn_frame.pack(padx=16, pady=16)
def make_btn(text, cmd, color=BTN_BG):
btn = tk.Button(btn_frame, text=text, command=cmd, bg=color, fg="white",
font=("Segoe UI", 11, "bold"), relief="flat", height=2, width=20)
btn.bind("<Enter>", lambda e: btn.config(bg=BTN_HOVER))
btn.bind("<Leave>", lambda e: btn.config(bg=color))
btn.bind("<ButtonPress-1>", lambda e: btn.config(bg=BTN_ACTIVE))
btn.bind("<ButtonRelease-1>", lambda e: btn.config(bg=BTN_HOVER))
return btn
# Buttons
make_btn("Roll Dice", self.roll_dice, ACCENT).pack(pady=8)
make_btn("Clear History", self.clear_history).pack(pady=8)
make_btn("About", self.show_about).pack(pady=20)
Explanation:
bind() โ allows hover and click effects
command โ runs a method when the button is clicked
make_btn โ re-usable button creator
Step 6: Create the Right Panel (Game UI)
The right panel shows dice results, winner, and history.
# RIGHT PANEL
right = tk.Frame(root, bg=APP_BG)
right.pack(side="right", fill="both", expand=True)
self.result_card = tk.Frame(right, bg=PANEL_BG, bd=2, relief="ridge")
self.result_card.pack(padx=30, pady=20, fill="both", expand=True)
tk.Label(self.result_card, text="Dice Game vs Computer", bg=PANEL_BG, fg=TEXT_CLR, font=("Segoe UI", 18, "bold")).pack(pady=20)
Explanation:
bd=2, relief="ridge" โ adds a border effect
fill="both", expand=True โ allows the frame to stretch
Step 7: Add Dice Result Labels
We'll display Player Roll, Computer Roll, and Winner.
# Player Roll
tk.Label(self.result_card, text="Your Roll:", bg=PANEL_BG, fg=SUBTEXT_CLR).pack()
self.player_label = tk.Label(self.result_card, text="--", bg=PANEL_BG, fg=ACCENT, font=("Segoe UI", 18, "bold"))
self.player_label.pack()
# Computer Roll
tk.Label(self.result_card, text="Computer Roll:", bg=PANEL_BG, fg=SUBTEXT_CLR).pack()
self.computer_label = tk.Label(self.result_card, text="--", bg=PANEL_BG, fg=ACCENT, font=("Segoe UI", 18, "bold"))
self.computer_label.pack()
# Winner
tk.Label(self.result_card, text="Winner:", bg=PANEL_BG, fg=SUBTEXT_CLR).pack()
self.winner_label = tk.Label(self.result_card, text="--", bg=PANEL_BG, fg=ACCENT, font=("Segoe UI", 18, "bold"))
self.winner_label.pack()
Explanation:
Initial values are set to "--"
Weโll update them after each dice roll
Step 8: Add Game History
We'll keep track of the last 10 rolls using a Listbox.
self.history_box = tk.Listbox(self.result_card, bg=INPUT_BG, fg=INPUT_FG, font=("Segoe UI", 12), height=10)
self.history_box.pack(fill="both", padx=10, pady=10)
self.history = [] # store history
Explanation:
Listbox โ shows a list of previous rolls
self.history โ stores history internally
Step 9: Add Game Logic
Now let's define the dice roll and history update methods.
def roll_dice(self):
player_roll = random.randint(1, 6)
computer_roll = random.randint(1, 6)
self.player_label.config(text=str(player_roll))
self.computer_label.config(text=str(computer_roll))
if player_roll > computer_roll:
winner = "You Win!"
elif player_roll < computer_roll:
winner = "Computer Wins!"
else:
winner = "Draw!"
self.winner_label.config(text=winner)
entry = f"Player: {player_roll} | Computer: {computer_roll} -> {winner}"
self.history.append(entry)
self.history = self.history[-10:]
self.update_history()
def update_history(self):
self.history_box.delete(0, tk.END)
for item in self.history:
self.history_box.insert(tk.END, item)
def clear_history(self):
self.history = []
self.update_history()
self.player_label.config(text="--")
self.computer_label.config(text="--")
self.winner_label.config(text="--")
def show_about(self):
messagebox.showinfo("About", "MateTools โ Dice Game\n\nPlay against the computer!")
Explanation:
random.randint(1, 6) โ simulates a dice roll
self.update_history() โ refreshes the Listbox
clear_history() โ resets all labels and history
show_about() โ opens an info dialog
Step 10: Run the App
Finally, initialize Tkinter and run the game.
if __name__ == "__main__":
root = tk.Tk()
app = DiceGameApp(root)
root.mainloop()
Explanation:
root = tk.Tk() โ creates the main window
root.mainloop() โ starts the GUI event loop
โ Result: You now have a fully functional Dice Game with a modern dark-themed GUI, history tracking, and interactive buttons!

Top comments (0)