使用Python實(shí)現(xiàn)一個(gè)網(wǎng)址管理小工具
這個(gè)程序是一個(gè)網(wǎng)址管理工具,主要用途是幫助用戶整理、存儲(chǔ)和快速訪問常用網(wǎng)站信息,具體功能包括:
- 集中管理網(wǎng)站信息:可記錄網(wǎng)站名稱、URL 地址和備注(如網(wǎng)站用途、賬號(hào)信息等),避免零散記憶或查找的麻煩。
- 便捷操作:支持添加、編輯、刪除網(wǎng)站信息,選中網(wǎng)站后可直接在瀏覽器中打開。
- 數(shù)據(jù)本地保存:所有信息存儲(chǔ)在本地 JSON 文件中,無需聯(lián)網(wǎng),保護(hù)隱私且數(shù)據(jù)不會(huì)丟失(除非手動(dòng)刪除文件)。
適合需要管理多個(gè)常用網(wǎng)站(如學(xué)習(xí)平臺(tái)、工作工具、常用服務(wù)等)的用戶,相當(dāng)于一個(gè)個(gè)性化的 “網(wǎng)站收藏夾”,比瀏覽器自帶收藏夾更靈活(可添加自定義備注)。
特別說明:第一次運(yùn)行時(shí),由于websites.json不存在,會(huì)自動(dòng)加載預(yù)設(shè)的默認(rèn)網(wǎng)站數(shù)據(jù)。當(dāng)新增、編輯、刪除網(wǎng)站,會(huì)于當(dāng)前目錄下建立本地文件:websites.json。之后就會(huì)加載使用websites.json的數(shù)據(jù)。
本程序需要的模塊tkinter、webbrowser、json、os是 Python 默認(rèn)包含的基礎(chǔ)庫,安裝 Python 時(shí)已自動(dòng)附帶,無需額外安裝。
運(yùn)行界面如下:


源碼如下:
import tkinter as tk
from tkinter import ttk, messagebox #, scrolledtext
import webbrowser
import json
import os
class WebsiteManager:
def __init__(self, root):
self.root = root
self.root.title("網(wǎng)址管理器")
self.root.geometry("900x600") # 增加寬度以容納備注列
# 存儲(chǔ)網(wǎng)站數(shù)據(jù)
self.websites = []
# 加載保存的數(shù)據(jù)
self.load_data()
# 創(chuàng)建界面
self.create_widgets()
def create_widgets(self):
# 頂部標(biāo)題
title_label = tk.Label(self.root, text="網(wǎng)址管理器", font=("Arial", 16, "bold"))
title_label.pack(pady=10)
# 允許編輯復(fù)選框
self.edit_var = tk.BooleanVar()
self.edit_check = tk.Checkbutton(self.root, text="允許編輯", variable=self.edit_var,
command=self.toggle_editing)
self.edit_check.pack(anchor="w", padx=20)
# 輸入?yún)^(qū)域框架
input_frame = tk.Frame(self.root)
input_frame.pack(fill="x", padx=20, pady=10)
# 網(wǎng)站名稱
tk.Label(input_frame, text="網(wǎng)站名稱:").grid(row=0, column=0, sticky="w", pady=5)
self.name_entry = tk.Entry(input_frame, width=42)
self.name_entry.grid(row=0, column=1, sticky="w", pady=5, padx=5)
# URL
tk.Label(input_frame, text="URL:").grid(row=1, column=0, sticky="w", pady=5)
self.url_entry = tk.Entry(input_frame, width=84)
self.url_entry.grid(row=1, column=1, sticky="w", pady=5, padx=5)
# 備注
tk.Label(input_frame, text="備注").grid(row=2, column=0, sticky="nw", pady=5)
# self.notes_text = scrolledtext.ScrolledText(input_frame, width=30, height=4)
self.notes_text = tk.Entry(input_frame, width=84)
self.notes_text.grid(row=2, column=1, sticky="w", pady=5, padx=5)
# 網(wǎng)站列表框架
list_frame = tk.Frame(self.root)
list_frame.pack(fill="both", expand=True, padx=20, pady=10)
# 創(chuàng)建Treeview來顯示網(wǎng)站列表,增加備注列
columns = ("名稱", "URL", "備注") # 增加備注列
self.website_list = ttk.Treeview(list_frame, columns=columns, show="headings", height=10)
# 設(shè)置列標(biāo)題
self.website_list.heading("名稱", text="網(wǎng)址名稱")
self.website_list.heading("URL", text="URL")
self.website_list.heading("備注", text="備注") # 增加備注列標(biāo)題
# 設(shè)置列寬,調(diào)整寬度總和超過列表框?qū)挾纫约せ钏綕L動(dòng)
self.website_list.column("名稱", width=150, minwidth=150)
self.website_list.column("URL", width=450, minwidth=450)
self.website_list.column("備注", width=400, minwidth=400) # 增加備注列
# 添加滾動(dòng)條
v_scrollbar = ttk.Scrollbar(list_frame, orient="vertical", command=self.website_list.yview)
h_scrollbar = ttk.Scrollbar(list_frame, orient="horizontal", command=self.website_list.xview)
self.website_list.configure(yscrollcommand=v_scrollbar.set, xscrollcommand=h_scrollbar.set)
# 布局Treeview和滾動(dòng)條
self.website_list.grid(row=0, column=0, sticky="nsew")
v_scrollbar.grid(row=0, column=1, sticky="ns")
h_scrollbar.grid(row=1, column=0, sticky="ew")
# 配置網(wǎng)格權(quán)重
list_frame.grid_rowconfigure(0, weight=1)
list_frame.grid_columnconfigure(0, weight=1)
# 綁定選擇事件
self.website_list.bind("<<TreeviewSelect>>", self.on_select)
# 按鈕框架
button_frame = tk.Frame(self.root)
button_frame.pack(fill="x", padx=20, pady=10)
# 左側(cè)按鈕
tk.Button(button_frame, text="打開選定網(wǎng)站", command=self.open_website).pack(side="left", padx=5)
tk.Button(button_frame, text="清空", command=self.clear_fields).pack(side="left", padx=5)
tk.Button(button_frame, text="幫助", command=self.show_help).pack(side="left", padx=5)
# 右側(cè)按鈕
tk.Button(button_frame, text="添加", command=self.add_website).pack(side="right", padx=5)
tk.Button(button_frame, text="編輯", command=self.edit_website).pack(side="right", padx=5)
tk.Button(button_frame, text="刪除", command=self.delete_website).pack(side="right", padx=5)
# 初始化網(wǎng)站列表
self.refresh_list()
# 初始狀態(tài)設(shè)置為不可編輯
self.toggle_editing()
def toggle_editing(self):
state = "normal" if self.edit_var.get() else "disabled"
self.name_entry.config(state=state)
self.url_entry.config(state=state)
self.notes_text.config(state=state)
def on_select(self, event):
selected = self.website_list.selection()
if selected:
item = selected[0]
values = self.website_list.item(item, "values")
# 調(diào)整查詢條件,包含備注字段
website = next((w for w in self.websites if w["name"] == values[0] and w["url"] == values[1]), None)
if website:
self.name_entry.config(state="normal")
self.url_entry.config(state="normal")
self.notes_text.config(state="normal")
self.name_entry.delete(0, tk.END)
self.name_entry.insert(0, website["name"])
self.url_entry.delete(0, tk.END)
self.url_entry.insert(0, website["url"])
#self.notes_text.delete(1.0, tk.END)
#self.notes_text.insert(1.0, website.get("notes", ""))
self.notes_text.delete(0, tk.END)
self.notes_text.insert(0, website["notes"])
self.toggle_editing()
def clear_fields(self):
self.name_entry.delete(0, tk.END)
self.url_entry.delete(0, tk.END)
#self.notes_text.delete(1.0, tk.END)
self.notes_text.delete(0, tk.END)
def open_website(self):
selected = self.website_list.selection()
if selected:
item = selected[0]
url = self.website_list.item(item, "values")[1]
webbrowser.open(url)
else:
messagebox.showwarning("警告", "請(qǐng)先選擇一個(gè)網(wǎng)站")
def add_website(self):
name = self.name_entry.get().strip()
url = self.url_entry.get().strip()
#notes = self.notes_text.get(1.0, tk.END).strip()
notes = self.notes_text.get().strip()
if not name or not url:
messagebox.showwarning("警告", "網(wǎng)站名稱和URL不能為空")
return
# 檢查URL是否已存在
if any(w["url"] == url for w in self.websites):
messagebox.showwarning("警告", "該URL已存在")
return
self.websites.append({
"name": name,
"url": url,
"notes": notes
})
self.refresh_list()
self.clear_fields()
self.save_data()
messagebox.showinfo("成功", "網(wǎng)站已添加")
def edit_website(self):
selected = self.website_list.selection()
if not selected:
messagebox.showwarning("警告", "請(qǐng)先選擇一個(gè)網(wǎng)站進(jìn)行編輯")
return
name = self.name_entry.get().strip()
url = self.url_entry.get().strip()
notes = self.notes_text.get(1.0, tk.END).strip()
if not name or not url:
messagebox.showwarning("警告", "網(wǎng)站名稱和URL不能為空")
return
item = selected[0]
old_values = self.website_list.item(item, "values")
old_url = old_values[1]
# 檢查URL是否已存在(排除自己)
if any(w["url"] == url and w["url"] != old_url for w in self.websites):
messagebox.showwarning("警告", "該URL已存在")
return
# 更新網(wǎng)站信息
for website in self.websites:
if website["url"] == old_url:
website["name"] = name
website["url"] = url
website["notes"] = notes
break
self.refresh_list()
self.save_data()
messagebox.showinfo("成功", "網(wǎng)站信息已更新")
def delete_website(self):
selected = self.website_list.selection()
if not selected:
messagebox.showwarning("警告", "請(qǐng)先選擇一個(gè)網(wǎng)站")
return
if messagebox.askyesno("確認(rèn)", "確定要?jiǎng)h除選定的網(wǎng)站嗎?"):
item = selected[0]
url = self.website_list.item(item, "values")[1]
# 從列表中刪除
self.websites = [w for w in self.websites if w["url"] != url]
self.refresh_list()
self.clear_fields()
self.save_data()
messagebox.showinfo("成功", "網(wǎng)站已刪除")
def refresh_list(self):
# 清空列表
for item in self.website_list.get_children():
self.website_list.delete(item)
# 添加網(wǎng)站到列表,包含備注信息
for website in self.websites:
self.website_list.insert("", "end", values=(
website["name"],
website["url"],
website.get("notes", "") # 增加備注顯示
))
def show_help(self):
help_text = """
網(wǎng)站管理器使用說明:
1. 勾選"允許編輯"復(fù)選框后,可以編輯網(wǎng)站名稱、URL和備注字段
2. 在右側(cè)列表中選擇一個(gè)網(wǎng)站,其信息將顯示在左側(cè)
3. 點(diǎn)擊"打開選定網(wǎng)站"將在瀏覽器中打開選中的網(wǎng)站
4. 點(diǎn)擊"清空"將清除所有輸入字段
5. 點(diǎn)擊"添加"將新網(wǎng)站添加到列表中(URL不能重復(fù))
6. 點(diǎn)擊"編輯"將修改選中的網(wǎng)站信息
7. 點(diǎn)擊"刪除"將從列表中移除選中的網(wǎng)站
注意:第一次運(yùn)行時(shí),由于websites.json不存在,會(huì)自動(dòng)加載預(yù)設(shè)的默認(rèn)網(wǎng)站數(shù)據(jù)。當(dāng)新增、編輯、刪除網(wǎng)站,會(huì)于當(dāng)前目錄下建立本地文件:websites.json。之后就會(huì)加載使用websites.json的數(shù)據(jù)。
"""
messagebox.showinfo("幫助", help_text)
def save_data(self):
with open("websites.json", "w", encoding="utf-8") as f:
json.dump(self.websites, f, ensure_ascii=False, indent=2)
def load_data(self):
# 如果文件存在,加載數(shù)據(jù)
if os.path.exists("websites.json"):
try:
with open("websites.json", "r", encoding="utf-8") as f:
self.websites = json.load(f)
except:
# 若文件損壞或格式錯(cuò)誤,初始化空列表
self.websites = []
else:
# 否則使用默認(rèn)數(shù)據(jù)
self.websites = [
{"name": "百度AI", "url": "https://chat.baidu.com", "notes": "百度智能對(duì)話平臺(tái)"},
{"name": "deepseekAI", "url": "https://chat.deepseek.com", "notes": "深度求索deepseekAI網(wǎng)頁版入口 "},
{"name": "豆包AI", "url": "https://www.doubao.com/chat", "notes": "字節(jié)跳動(dòng)旗下AI 助手網(wǎng)頁版入口"},
{"name": "B站", "url": "https://www.bilibili.com", "notes": "視頻分享平臺(tái),有很多學(xué)習(xí)資源"}
]
if __name__ == "__main__":
root = tk.Tk()
app = WebsiteManager(root)
root.mainloop()到此這篇關(guān)于使用Python實(shí)現(xiàn)一個(gè)網(wǎng)址管理小工具的文章就介紹到這了,更多相關(guān)Python網(wǎng)址管理工具內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Centos下實(shí)現(xiàn)安裝Python3.6和Python2共存
這篇文章主要介紹了Centos下實(shí)現(xiàn)安裝Python3.6和Python2共存,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
Python實(shí)現(xiàn)在PDF中插入單圖像水印和平鋪圖像水印
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)在PDF中插入單圖像水印和平鋪圖像水印,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
python中numpy數(shù)組與list相互轉(zhuǎn)換實(shí)例方法
在本篇文章里小編給大家整理的是一篇關(guān)于python中numpy數(shù)組與list相互轉(zhuǎn)換實(shí)例方法,對(duì)此有興趣的朋友們可以學(xué)習(xí)下。2021-01-01
利用Python實(shí)現(xiàn)個(gè)性化日歷
雖然市面上已經(jīng)存在現(xiàn)成的日歷功能,并且有第三方庫可以直接調(diào)用實(shí)現(xiàn),但我們?nèi)匀幌Mㄟ^自己編寫日歷程序來引出我認(rèn)為好用的日歷實(shí)現(xiàn),所以下面就跟隨小編一起學(xué)習(xí)一下如何使用Python編寫一個(gè)簡(jiǎn)單的日歷程序吧2024-02-02
Python數(shù)據(jù)分析處理(三)--運(yùn)動(dòng)員信息的分組與聚合
這篇文章主要介紹了Python數(shù)據(jù)清洗與處理?運(yùn)動(dòng)員信息的分組與聚合,根據(jù)Python數(shù)據(jù)清洗與處理?的相關(guān)資料展開運(yùn)動(dòng)員信息的分組與聚合的文章內(nèi)容,需要的朋友可以參考一下2021-12-12

