Thursday, July 9, 2026

Tahsin's Tiny Virtual Operating System (written in Python; Features: Text Editor, Calculator, File Explorer and Terminal)

import tkinter as tk

from tkinter import ttk, filedialog, messagebox

import os


class VirtualOS:

    def __init__(self, root):

        self.root = root

        self.root.title("Virtual OS")

        self.root.geometry("800x600")


        # Desktop background

        self.desktop = tk.Frame(self.root, bg="lightblue")

        self.desktop.pack(fill="both", expand=True)


        # Taskbar

        self.taskbar = tk.Frame(self.root, bg="gray", height=30)

        self.taskbar.pack(side="bottom", fill="x")


        # Taskbar buttons

        tk.Button(self.taskbar, text="Text Editor", command=self.open_text_editor).pack(side="left")

        tk.Button(self.taskbar, text="Calculator", command=self.open_calculator).pack(side="left")

        tk.Button(self.taskbar, text="File Explorer", command=self.open_file_explorer).pack(side="left")

        tk.Button(self.taskbar, text="Terminal", command=self.open_terminal).pack(side="left")


    # --- Applications ---

    def open_text_editor(self):

        win = tk.Toplevel(self.root)

        win.title("Text Editor")

        text = tk.Text(win, wrap="word")

        text.pack(fill="both", expand=True)


        def save_file():

            f = filedialog.asksaveasfilename(defaultextension=".txt")

            if f:

                with open(f, "w") as file:

                    file.write(text.get("1.0", "end-1c"))


        tk.Button(win, text="Save", command=save_file).pack()


    def open_calculator(self):

        win = tk.Toplevel(self.root)

        win.title("Calculator")


        entry = tk.Entry(win, width=20)

        entry.pack()


        def calculate():

            try:

                result = eval(entry.get())

                messagebox.showinfo("Result", f"Answer: {result}")

            except Exception as e:

                messagebox.showerror("Error", str(e))


        tk.Button(win, text="Calculate", command=calculate).pack()


    def open_file_explorer(self):

        win = tk.Toplevel(self.root)

        win.title("File Explorer")


        tree = ttk.Treeview(win)

        tree.pack(fill="both", expand=True)


        path = os.getcwd()

        tree.insert("", "end", text=path, open=True)


        for item in os.listdir(path):

            tree.insert("", "end", text=item)


    def open_terminal(self):

        win = tk.Toplevel(self.root)

        win.title("Terminal")


        output = tk.Text(win, height=15, bg="black", fg="white")

        output.pack(fill="both", expand=True)


        entry = tk.Entry(win)

        entry.pack(fill="x")


        def run_command(event=None):

            cmd = entry.get()

            entry.delete(0, "end")

            if cmd == "ls":

                output.insert("end", "\n".join(os.listdir(os.getcwd())) + "\n")

            elif cmd.startswith("cd "):

                try:

                    os.chdir(cmd.split(" ", 1)[1])

                    output.insert("end", f"Changed directory to {os.getcwd()}\n")

                except Exception as e:

                    output.insert("end", f"Error: {e}\n")

            elif cmd == "pwd":

                output.insert("end", os.getcwd() + "\n")

            else:

                output.insert("end", f"Unknown command: {cmd}\n")


        entry.bind("<Return>", run_command)


# --- Run Virtual OS ---

if __name__ == "__main__":

    root = tk.Tk()

    os_system = VirtualOS(root)

    root.mainloop()


No comments:

Post a Comment

Tahsin's Python Table Tennis Game App

import tkinter as tk import random import os import platform # --- Sound function --- def play_background_sound():     try:         if platf...