python WEBブラウザー

import tkinter as tk
from tkinter import ttk, messagebox
import webbrowser
import os

class WebBrowser(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Web Browser")
        self.geometry("800x600")

        self.notebook = ttk.Notebook(self)
        self.notebook.pack(fill="both", expand=True)

        self.tabs = []

        self.add_new_tab()

    def add_new_tab(self):
        tab = ttk.Frame(self.notebook)
        self.notebook.add(tab, text="New Tab")

        url_entry = ttk.Entry(tab, width=70)
        url_entry.grid(row=0, column=1, padx=5, pady=5, sticky="ew")
        url_entry.bind("<Return>", lambda event: self.open_webpage(url_entry.get()))

        go_button = ttk.Button(tab, text="Go", command=lambda: self.open_webpage(url_entry.get()))
        go_button.grid(row=0, column=2, padx=5, pady=5)

        back_button = ttk.Button(tab, text="Back", command=lambda: self.back(tab))
        back_button.grid(row=0, column=0, padx=5, pady=5)

        forward_button = ttk.Button(tab, text="Forward", command=lambda: self.forward(tab))
        forward_button.grid(row=0, column=3, padx=5, pady=5)

        close_button = ttk.Button(tab, text="X", command=lambda: self.close_tab(tab))
        close_button.grid(row=0, column=4, padx=5, pady=5)

        progress_bar = ttk.Progressbar(tab, orient="horizontal", mode="indeterminate")
        progress_bar.grid(row=1, column=1, columnspan=2, sticky="ew")

        web_browser = ttk.Frame(tab)
        web_browser.grid(row=2, column=0, columnspan=5, padx=5, pady=5, sticky="nsew")

        self.tabs.append({
            "tab": tab,
            "url_entry": url_entry,
            "go_button": go_button,
            "back_button": back_button,
            "forward_button": forward_button,
            "close_button": close_button,
            "progress_bar": progress_bar,
            "web_browser": web_browser,
            "browser": webbrowser.get(),
            "current_page": None
        })

    def open_webpage(self, url):
        if not url.startswith("http://") and not url.startswith("https://"):
            url = "http://" + url

        tab = self.notebook.select()
        tab_index = self.notebook.index(tab)
        tab_info = self.tabs[tab_index]

        tab_info["browser"].open_new(url)
        tab_info["current_page"] = url

    def back(self, tab):
        tab_index = self.notebook.index(tab)
        tab_info = self.tabs[tab_index]
        tab_info["browser"].back()

    def forward(self, tab):
        tab_index = self.notebook.index(tab)
        tab_info = self.tabs[tab_index]
        tab_info["browser"].forward()

    def close_tab(self, tab):
        tab_index = self.notebook.index(tab)
        self.notebook.forget(tab_index)
        del self.tabs[tab_index]

if __name__ == "__main__":
    app = WebBrowser()
    app.mainloop()

投稿者: chosuke

趣味はゲームやアニメや漫画などです

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です