你是想 在 Debian 上写一个类似 Windows Notepad(记事本)的文本编辑器程序,对吧?下面我用几种常见方式来说明,从简单到正式都有。
如果你只是想要一个能打开、编辑、保存文本的小工具:
#!/bin/bash
file="$1"
if [ -z "$file" ]; then
file="untitled.txt"
fi
nano "$file"
保存为 notepad.sh:
chmod +x notepad.sh
./notepad.sh test.txt
实际上调用的是
nano,但你可以把它当“命令行 Notepad”。
Debian 自带 Python,用 tkinter 即可。
sudo apt update
sudo apt install python3-tk
import tkinter as tk
from tkinter import filedialog
def open_file():
path = filedialog.askopenfilename()
if path:
with open(path) as f:
text.delete(1.0, tk.END)
text.insert(tk.END, f.read())
def save_file():
path = filedialog.asksaveasfilename()
if path:
with open(path, "w") as f:
f.write(text.get(1.0, tk.END))
root = tk.Tk()
root.title("Debian Notepad")
text = tk.Text(root)
text.pack(expand=1, fill="both")
menu = tk.Menu(root)
root.config(menu=menu)
file_menu = tk.Menu(menu)
menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
root.mainloop()
运行:
python3 notepad.py
sudo apt install libgtk-3-dev
(代码较长,适合系统级学习,如果你需要我可以单独写)
如果你只是想 用 Notepad,而不是写:
gedit(GNOME)mousepad(XFCE)kate(KDE)leafpad安装示例:
sudo apt install gedit
如果你愿意,可以告诉我:
我可以直接给你完整可运行代码。