新手第一次写代码老手勿喷,仅供学习~
帕克大人/帕克大人_1 - 码云 - 开源中国 (gitee.com)
先看软件功能:
1、主界面只有新建按钮
2、点击新建按钮后出现两个文本框,用来将代码写在上面下面是命名生成按钮,
3、输入完成后点击保存
4、就生成了一个按钮,当我们需要这段代码是点击这个按钮,这段语句就会被复制到剪贴板上,用鼠标右击这个按钮时会出现删除按钮用来删掉这段代码,这就完了 只有再打包后能正常保存使用。
下面是代码。
import tkinter as tk
from tkinter import messagebox
import tkinter.ttk as ttk
import pyperclip
import pickle
class CodeSaver:
def __init__(self, root):
self.root = root
self.root.title("CodeSaver")
self.root.geometry("600x400")
self.root.configure(bg='#073642')
self.saved_code = {}
self.new_window = None
self.tool_bar = tk.Frame(root)
self.tool_bar.pack(side="left", fill="y", padx=10, pady=10)
self.scrollbar = tk.Scrollbar(root, orient="vertical")
self.scrollbar.pack(side="right", fill="y")
self.canvas = tk.Canvas(root, bg="#073642", yscrollcommand=self.scrollbar.set)
self.canvas.pack(side="left", fill="both", expand=True)
self.scrollbar.config(command=self.canvas.yview)
self.container = tk.Frame(self.canvas, bg="#073642")
self.canvas.create_window((0, 0), window=self.container, anchor="nw")
self.container.bind("<Configure>", lambda event: self.canvas.configure(scrollregion=self.canvas.bbox("all")))
self.new_button = ttk.Button(self.tool_bar, text="新建", command=self.open_new_window, style="ToolButton.TButton")
self.new_button.pack(side="top", padx=5, pady=5)
style = ttk.Style()
style.configure("ToolButton.TButton",
font=("黑体", 14),
foreground="black",
background="#073642",
padding=10,
relief="raised")
style.configure("TLabel",
font=("黑体", 14),
foreground="white",
background="#073642")
style.configure("TEntry",
font=("黑体", 14),
background="white",
insertbackground="white",
selectbackground="#073642",
selectforeground="white")
self.load_saved_code() # 加载已保存的代码
def load_saved_code(self):
try:
with open("saved_code.pickle", "rb") as f:
self.saved_code = pickle.load(f)
self.create_code_buttons()
except FileNotFoundError:
self.saved_code = {}
def save_code(self):
with open("saved_code.pickle", "wb") as f:
pickle.dump(self.saved_code, f)
def open_new_window(self):
if self.new_window is not None:
return
self.new_window = tk.Toplevel(self.root)
self.new_window.title("新建代码")
self.new_window.configure(bg="#001A34")
code_label = ttk.Label(self.new_window, text="代码:", style="TLabel")
code_label.pack(pady=5)
self.code_text = tk.Text(self.new_window, height=10, width=40)
self.code_text.pack()
name_label = ttk.Label(self.new_window, text="按钮名称:", style="TLabel")
name_label.pack(pady=5)
self.name_entry = ttk.Entry(self.new_window, width=30)
self.name_entry.pack()
save_button = ttk.Button(self.new_window, text="保存", command=self.save_new_code, style="TButton")
save_button.pack(pady=10)
self.new_window.protocol("WM_DELETE_WINDOW", self.close_new_window)
def save_new_code(self):
code = self.code_text.get(1.0, tk.END).strip()
name = self.name_entry.get().strip()
if code == "" or name == "":
messagebox.showwarning("警告", "请输入代码和按钮名称")
return
self.saved_code[name] = code
self.save_code()
self.create_code_button(name, code)
self.close_new_window()
def create_code_buttons(self):
for name, code in self.saved_code.items():
self.create_code_button(name, code)
def create_code_button(self, name, code):
code_button = CodeButton(self.container, name, code, self.delete_code, style="ToolButton.TButton")
code_button.pack(side="top", padx=5, pady=5)
def delete_code(self, code_button):
name = code_button.name
confirmed = messagebox.askyesno("确认删除", f"确定要删除按钮 '{name}' 吗?")
if confirmed:
del self.saved_code[name]
self.save_code()
code_button.destroy()
def close_new_window(self):
self.new_window.destroy()
self.new_window = None
class CodeButton(ttk.Button):
def __init__(self, parent, name, code, delete_callback, **kwargs):
super().__init__(parent, text=name, command=lambda: self.copy_code(name, code), **kwargs)
self.name = name
self.code = code
self.delete_callback = delete_callback
self.bind("<Button-3>", self.show_delete_menu)
def copy_code(self, name, code):
pyperclip.copy(code)
messagebox.showinfo("提示", f"代码 '{name}' 已复制到剪贴板")
def show_delete_menu(self, event):
menu = tk.Menu(self, tearoff=0)
menu.add_command(label="删除", command=self.delete)
menu.post(event.x_root, event.y_root)
def delete(self):
self.delete_callback(self)
root = tk.Tk()
root.configure(bg="#073642")
code_saver = CodeSaver(root)
root.mainloop()