import tkinter as tk
from tkinter import filedialog, messagebox
import os
import subprocess
class GameLauncher:
def __init__(self, root):
self.root = root
self.root.title("🕹️ Python Game Launcher")
self.root.geometry("500x400")
self.root.config(bg="#1e1e1e")
self.games = []
self.label = tk.Label(root, text="🎮 Your Python Games", font=("Helvetica", 16), fg="white", bg="#1e1e1e")
self.label.pack(pady=10)
self.game_listbox = tk.Listbox(root, width=50, height=15, font=("Courier", 10))
self.game_listbox.pack(pady=10)
self.launch_button = tk.Button(root, text="🚀 Launch Game", command=self.launch_game, bg="#28a745", fg="white", font=("Helvetica", 12))
self.launch_button.pack(pady=5)
self.load_button = tk.Button(root, text="📂 Load Games Folder", command=self.load_games, bg="#007bff", fg="white", font=("Helvetica", 12))
self.load_button.pack(pady=5)
def load_games(self):
folder_path = filedialog.askdirectory(title="Select Game Folder")
if not folder_path:
return
self.games = []
self.game_listbox.delete(0, tk.END)
for file in os.listdir(folder_path):
if file.endswith(".py"):
self.games.append(os.path.join(folder_path, file))
self.game_listbox.insert(tk.END, file)
if not self.games:
messagebox.showinfo("No Games Found", "No Python (.py) files found in the selected folder.")
def launch_game(self):
selected_index = self.game_listbox.curselection()
if not selected_index:
messagebox.showwarning("No Selection", "Please select a game to launch.")
return
game_path = self.games[selected_index[0]]
try:
subprocess.Popen(["python", game_path], shell=True)
except Exception as e:
messagebox.showerror("Error", f"Failed to launch the game:\n{e}")
# Run the launcher
if __name__ == "__main__":
root = tk.Tk()
app = GameLauncher(root)
root.mainloop()
No comments:
Post a Comment