给以下代码添加"储存功能"再次打开时显示历史记录,并且将Python按钮和图形化界面内容显示中文,开启后自动关闭命令行窗口以下是代码内容:import tkinter as tk from tkinter import filedialog, messagebox import datetime
class IPFilterApp: def init(self, root): self.root = root self.root.title("IP Filter")
self.ip_list = []
self.whitelist = []
self.blacklist = []
self.create_widgets()
def create_widgets(self):
# IP Entry
self.ip_label = tk.Label(self.root, text="IP Address:")
self.ip_label.grid(row=0, column=0, padx=5, pady=5)
self.ip_entry = tk.Entry(self.root)
self.ip_entry.grid(row=0, column=1, padx=5, pady=5)
# Add IP Button
self.add_ip_button = tk.Button(self.root, text="Add IP", command=self.add_ip)
self.add_ip_button.grid(row=0, column=2, padx=5, pady=5)
# Import IP Button
self.import_ip_button = tk.Button(self.root, text="Import IPs", command=self.import_ips)
为了实现你所描述的功能,我们需要做以下几件事:
以下是修改后的代码:
import tkinter as tk
from tkinter import filedialog, messagebox
import datetime
import os
import json
class IPFilterApp:
def __init__(self, root):
self.root = root
self.root.title("IP过滤器")
self.ip_list = []
self.whitelist = []
self.blacklist = []
self.load_data()
self.create_widgets()
self.display_history()
def create_widgets(self):
# IP Entry
self.ip_label = tk.Label(self.root, text="IP地址:")
self.ip_label.grid(row=0, column=0, padx=5, pady=5)
self.ip_entry = tk.Entry(self.root)
self.ip_entry.grid(row=0, column=1, padx=5, pady=5)
# Add IP Button
self.add_ip_button = tk.Button(self.root, text="添加IP", command=self.add_ip)
self.add_ip_button.grid(row=0, column=2, padx=5, pady=5)
# Import IP Button
self.import_ip_button = tk.Button(self.root, text="导入IP", command=self.import_ips)
self.import_ip_button.grid(row=0, column=3, padx=5, pady=5)
# IP Listbox
self.ip_listbox = tk.Listbox(self.root)
self.ip_listbox.grid(row=1, column=0, columnspan=4, padx=5, pady=5)
# Whitelist and Blacklist Buttons
self.whitelist_button = tk.Button(self.root, text="添加到白名单", command=self.add_to_whitelist)
self.whitelist_button.grid(row=2, column=0, padx=5, pady=5)
self.blacklist_button = tk.Button(self.root, text="添加到黑名单", command=self.add_to_blacklist)
self.blacklist_button.grid(row=2, column=1, padx=5, pady=5)
# Save Button
self.save_button = tk.Button(self.root, text="保存", command=self.save_data)
self.save_button.grid(row=2, column=2, padx=5, pady=5)
# Exit Button
self.exit_button = tk.Button(self.root, text="退出", command=self.root.quit)
self.exit_button.grid(row=2, column=3, padx=5, pady=5)
def add_ip(self):
ip = self.ip_entry.get()
if ip:
self.ip_list.append(ip)
self.ip_listbox.insert(tk.END, ip)
self.ip_entry.delete(0, tk.END)
def import_ips(self):
file_path = filedialog.askopenfilename()
if file_path:
with open(file_path, 'r') as file:
ips = file.readlines()
for ip in ips:
ip = ip.strip()
if ip:
self.ip_list.append(ip)
self.ip_listbox.insert(tk.END, ip)
def add_to_whitelist(self):
selected_ip = self.ip_listbox.get(tk.ACTIVE)
if selected_ip and selected_ip not in self.whitelist:
self.whitelist.append(selected_ip)
messagebox.showinfo("信息", f"{selected_ip} 已添加到白名单")
def add_to_blacklist(self):
selected_ip = self.ip_listbox.get(tk.ACTIVE)
if selected_ip and selected_ip not in self.blacklist:
self.blacklist.append(selected_ip)
messagebox.showinfo("信息", f"{selected_ip} 已添加到黑名单")
def save_data(self):
data = {
'ip_list': self.ip_list,
'whitelist': self.whitelist,
'blacklist': self.blacklist
}
with open('ip_data.json', 'w') as file:
json.dump(data, file)
messagebox.showinfo("信息", "数据已保存")
def load_data(self):
if os.path.exists('ip_data.json'):
with open('ip_data.json', 'r') as file:
data = json.load(file)
self.ip_list = data.get('ip_list', [])
self.whitelist = data.get('whitelist', [])
self.blacklist = data.get('blacklist', [])
def display_history(self):
for ip in self.ip_list:
self.ip_listbox.insert(tk.END, ip)
if __name__ == "__main__":
root = tk.Tk()
app = IPFilterApp(root)
root.mainloop()
pyinstaller
将脚本打包为可执行文件,具体命令如下:
pyinstaller --noconsole -F your_script.py
这样生成的可执行文件在运行时不会显示命令行窗口。希望这些修改能满足你的需求。