|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import ttk, filedialog, messagebox |
| 3 | +import psutil |
| 4 | +import webview |
| 5 | +import time |
| 6 | +import os |
| 7 | +import requests |
| 8 | +from bleak import discover # Import bleak for Bluetooth functionality |
| 9 | + |
| 10 | +class AdvancedOS: |
| 11 | + def __init__(self, root): |
| 12 | + self.root = root |
| 13 | + self.root.title("Advanced OS") |
| 14 | + self.root.geometry("1200x800") |
| 15 | + self.root.config(bg="lightgray") |
| 16 | + |
| 17 | + # Default theme color |
| 18 | + self.theme_color = "lightblue" |
| 19 | + |
| 20 | + # Create taskbar |
| 21 | + self.create_taskbar() |
| 22 | + |
| 23 | + # Create desktop |
| 24 | + self.create_desktop() |
| 25 | + |
| 26 | + # Start system info updater |
| 27 | + self.start_system_info_updater() |
| 28 | + |
| 29 | + # Update clock every second |
| 30 | + self.update_clock() |
| 31 | + |
| 32 | + def create_taskbar(self): |
| 33 | + self.taskbar = tk.Frame(self.root, bg="black", height=40) |
| 34 | + self.taskbar.pack(side=tk.BOTTOM, fill=tk.X) |
| 35 | + |
| 36 | + # Start Menu Button |
| 37 | + self.start_btn = tk.Button(self.taskbar, text="Start", bg="darkgray", fg="white", command=self.show_start_menu) |
| 38 | + self.start_btn.pack(side=tk.LEFT, padx=5) |
| 39 | + |
| 40 | + # System Info |
| 41 | + self.sys_info_label = tk.Label(self.taskbar, text="Wi-Fi: -- | Bluetooth: -- | Battery: --%", bg="black", fg="white", font=("Arial", 10)) |
| 42 | + self.sys_info_label.pack(side=tk.LEFT, padx=10) |
| 43 | + |
| 44 | + # Clock |
| 45 | + self.clock_label = tk.Label(self.taskbar, text="", bg="black", fg="white", font=("Arial", 12)) |
| 46 | + self.clock_label.pack(side=tk.RIGHT, padx=10) |
| 47 | + |
| 48 | + def update_clock(self): |
| 49 | + current_time = time.strftime("%H:%M:%S") |
| 50 | + self.clock_label.config(text=current_time) |
| 51 | + self.root.after(1000, self.update_clock) |
| 52 | + |
| 53 | + def create_desktop(self): |
| 54 | + self.desktop = tk.Frame(self.root, bg=self.theme_color) |
| 55 | + self.desktop.pack(fill=tk.BOTH, expand=True) |
| 56 | + |
| 57 | + # Desktop icons |
| 58 | + icons = [ |
| 59 | + ("Notepad", self.open_text_editor), |
| 60 | + ("File Explorer", self.open_file_explorer), |
| 61 | + ("Calculator", self.open_calculator), |
| 62 | + ("Browser", self.open_browser), |
| 63 | + ("Python Console", self.open_python_console), |
| 64 | + ("Music Player", self.open_music_player), |
| 65 | + ("Task Manager", self.open_task_manager), |
| 66 | + ("Weather", self.open_weather_app), |
| 67 | + ] |
| 68 | + |
| 69 | + for i, (text, command) in enumerate(icons): |
| 70 | + icon = tk.Button(self.desktop, text=text, command=command, width=15, height=2) |
| 71 | + icon.place(x=50, y=50 + i * 70) |
| 72 | + |
| 73 | + def show_start_menu(self): |
| 74 | + start_menu = tk.Toplevel(self.root) |
| 75 | + start_menu.title("Start Menu") |
| 76 | + start_menu.geometry("200x300") |
| 77 | + start_menu.resizable(False, False) |
| 78 | + |
| 79 | + tk.Label(start_menu, text="Start Menu", font=("Arial", 14)).pack(pady=10) |
| 80 | + |
| 81 | + btn_settings = tk.Button(start_menu, text="Settings", command=self.open_settings) |
| 82 | + btn_settings.pack(fill=tk.X, pady=5) |
| 83 | + |
| 84 | + btn_exit = tk.Button(start_menu, text="Exit", command=self.root.quit) |
| 85 | + btn_exit.pack(fill=tk.X, pady=5) |
| 86 | + |
| 87 | + def open_calculator(self): |
| 88 | + calc = tk.Toplevel(self.root) |
| 89 | + calc.title("Calculator") |
| 90 | + calc.geometry("300x400") |
| 91 | + calc.resizable(False, False) |
| 92 | + |
| 93 | + def calculate(): |
| 94 | + try: |
| 95 | + result = eval(entry.get()) |
| 96 | + entry.delete(0, tk.END) |
| 97 | + entry.insert(tk.END, str(result)) |
| 98 | + except Exception: |
| 99 | + entry.delete(0, tk.END) |
| 100 | + entry.insert(tk.END, "Error") |
| 101 | + |
| 102 | + entry = tk.Entry(calc, font=("Arial", 18), justify="right") |
| 103 | + entry.pack(fill=tk.BOTH, padx=10, pady=10) |
| 104 | + |
| 105 | + btn_calc = tk.Button(calc, text="=", command=calculate, font=("Arial", 14)) |
| 106 | + btn_calc.pack(fill=tk.BOTH, padx=10, pady=10) |
| 107 | + |
| 108 | + def open_text_editor(self): |
| 109 | + editor = tk.Toplevel(self.root) |
| 110 | + editor.title("Text Editor") |
| 111 | + editor.geometry("600x400") |
| 112 | + editor.resizable(True, True) |
| 113 | + |
| 114 | + text_area = tk.Text(editor, font=("Arial", 12)) |
| 115 | + text_area.pack(fill=tk.BOTH, expand=True) |
| 116 | + |
| 117 | + def save_file(): |
| 118 | + file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt")]) |
| 119 | + if file_path: |
| 120 | + with open(file_path, "w") as file: |
| 121 | + file.write(text_area.get(1.0, tk.END)) |
| 122 | + messagebox.showinfo("File Saved", f"File saved at {file_path}") |
| 123 | + |
| 124 | + menu_bar = tk.Menu(editor) |
| 125 | + file_menu = tk.Menu(menu_bar, tearoff=0) |
| 126 | + file_menu.add_command(label="Save", command=save_file) |
| 127 | + file_menu.add_separator() |
| 128 | + file_menu.add_command(label="Exit", command=editor.destroy) |
| 129 | + menu_bar.add_cascade(label="File", menu=file_menu) |
| 130 | + |
| 131 | + editor.config(menu=menu_bar) |
| 132 | + |
| 133 | + def open_file_explorer(self): |
| 134 | + file_explorer = tk.Toplevel(self.root) |
| 135 | + file_explorer.title("File Explorer") |
| 136 | + file_explorer.geometry("500x300") |
| 137 | + |
| 138 | + def browse_files(): |
| 139 | + file_path = filedialog.askopenfilename() |
| 140 | + if file_path: |
| 141 | + messagebox.showinfo("File Selected", f"You selected: {file_path}") |
| 142 | + |
| 143 | + tk.Button(file_explorer, text="Browse Files", command=browse_files).pack(pady=20) |
| 144 | + |
| 145 | + def open_browser(self): |
| 146 | + # Launch the browser in the main thread to avoid the pywebview error |
| 147 | + webview.create_window("Advanced Browser", "https://www.google.com") |
| 148 | + webview.start() |
| 149 | + |
| 150 | + def open_music_player(self): |
| 151 | + music_player = tk.Toplevel(self.root) |
| 152 | + music_player.title("Music Player") |
| 153 | + music_player.geometry("300x200") |
| 154 | + |
| 155 | + def play_music(): |
| 156 | + messagebox.showinfo("Music Player", "Playing music...") |
| 157 | + |
| 158 | + def stop_music(): |
| 159 | + messagebox.showinfo("Music Player", "Music stopped.") |
| 160 | + |
| 161 | + tk.Button(music_player, text="Play", command=play_music).pack(pady=20) |
| 162 | + tk.Button(music_player, text="Stop", command=stop_music).pack(pady=5) |
| 163 | + |
| 164 | + def open_task_manager(self): |
| 165 | + task_manager = tk.Toplevel(self.root) |
| 166 | + task_manager.title("Task Manager") |
| 167 | + task_manager.geometry("400x300") |
| 168 | + |
| 169 | + processes = [proc.info['name'] for proc in psutil.process_iter(['name'])] |
| 170 | + processes_list = '\n'.join(processes) |
| 171 | + tk.Label(task_manager, text=f"Running Processes:\n{processes_list}", font=("Arial", 10)).pack(pady=10) |
| 172 | + |
| 173 | + def open_weather_app(self): |
| 174 | + weather_app = tk.Toplevel(self.root) |
| 175 | + weather_app.title("Weather") |
| 176 | + weather_app.geometry("400x300") |
| 177 | + |
| 178 | + city = "New York" |
| 179 | + api_key = "YOUR_API_KEY_HERE" # Replace with a valid OpenWeatherMap API key |
| 180 | + url = f"http://api.openweathermap.org/data/2.5/weather?q=udupi&appid={api_key}" |
| 181 | + |
| 182 | + response = requests.get(url) |
| 183 | + weather_data = response.json() |
| 184 | + |
| 185 | + if weather_data.get("cod") == 200: |
| 186 | + main_data = weather_data['main'] |
| 187 | + temp = main_data['temp'] - 273.15 # Convert Kelvin to Celsius |
| 188 | + weather_desc = weather_data['weather'][0]['description'] |
| 189 | + temp_text = f"Temperature: {temp:.2f}°C\nCondition: {weather_desc.capitalize()}" |
| 190 | + tk.Label(weather_app, text=temp_text, font=("Arial", 12)).pack(pady=20) |
| 191 | + else: |
| 192 | + tk.Label(weather_app, text="Failed to retrieve data.", font=("Arial", 12)).pack(pady=20) |
| 193 | + |
| 194 | + def open_settings(self): |
| 195 | + settings = tk.Toplevel(self.root) |
| 196 | + settings.title("Settings") |
| 197 | + settings.geometry("400x300") |
| 198 | + settings.resizable(False, False) |
| 199 | + |
| 200 | + tk.Label(settings, text="Settings", font=("Arial", 16)).pack(pady=10) |
| 201 | + |
| 202 | + def change_theme(color): |
| 203 | + self.theme_color = color |
| 204 | + self.desktop.config(bg=color) |
| 205 | + |
| 206 | + tk.Button(settings, text="Blue Theme", command=lambda: change_theme("lightblue")).pack(fill=tk.X, pady=5) |
| 207 | + tk.Button(settings, text="Gray Theme", command=lambda: change_theme("gray")).pack(fill=tk.X, pady=5) |
| 208 | + tk.Button(settings, text="Green Theme", command=lambda: change_theme("lightgreen")).pack(fill=tk.X, pady=5) |
| 209 | + |
| 210 | + def open_python_console(self): |
| 211 | + console = tk.Toplevel(self.root) |
| 212 | + console.title("Python Console") |
| 213 | + console.geometry("600x400") |
| 214 | + console.resizable(True, True) |
| 215 | + |
| 216 | + # Text area for console output |
| 217 | + text_area = tk.Text(console, font=("Courier", 12)) |
| 218 | + text_area.pack(fill=tk.BOTH, expand=True) |
| 219 | + |
| 220 | + def execute_command(): |
| 221 | + try: |
| 222 | + # Get user input and execute it |
| 223 | + code = input_area.get(1.0, tk.END) |
| 224 | + exec(code) |
| 225 | + output = eval(code) |
| 226 | + text_area.insert(tk.END, f"\n{output}\n") |
| 227 | + except Exception as e: |
| 228 | + text_area.insert(tk.END, f"Error: {e}\n") |
| 229 | + |
| 230 | + input_area = tk.Text(console, font=("Courier", 12), height=5) |
| 231 | + input_area.pack(fill=tk.X, padx=10, pady=10) |
| 232 | + |
| 233 | + # Execute button to run the Python code |
| 234 | + execute_btn = tk.Button(console, text="Execute", command=execute_command) |
| 235 | + execute_btn.pack(pady=5) |
| 236 | + |
| 237 | + def start_system_info_updater(self): |
| 238 | + def update_system_info(): |
| 239 | + battery = psutil.sensors_battery() |
| 240 | + wifi = "Connected" if psutil.net_if_addrs().get("Wi-Fi") else "Not Connected" |
| 241 | + bluetooth = self.get_bluetooth_status() # Get Bluetooth status using bleak |
| 242 | + battery_percent = battery.percent if battery else "--" |
| 243 | + self.sys_info_label.config(text=f"Wi-Fi: {wifi} | Bluetooth: {bluetooth} | Battery: {battery_percent}%") |
| 244 | + self.root.after(5000, update_system_info) |
| 245 | + |
| 246 | + update_system_info() |
| 247 | + |
| 248 | + def get_bluetooth_status(self): |
| 249 | + # Use bleak to check Bluetooth status |
| 250 | + devices = discover() |
| 251 | + if devices: |
| 252 | + return "On" |
| 253 | + else: |
| 254 | + return "Off" |
| 255 | + |
| 256 | +if __name__ == "__main__": |
| 257 | + root = tk.Tk() |
| 258 | + os_app = AdvancedOS(root) |
| 259 | + root.mainloop() |
0 commit comments