我有点像 python 菜鸟,我试图为我的学校制作一个简单的文本编辑器,但是保存、剪切、粘贴和复制不起作用。它称为 PyWriter,我使用 Tkinter。很抱歉没有添加代码注释。这是我的代码:
import Tkinter
from Tkinter import *
from ScrolledText import *
import tkFileDialog
import tkMessageBox
def __init__(self):
self.text = textPad()
self.text.pack()
root = Tkinter.Tk(className=" PyWriter")
textPad = ScrolledText(root, width=100, height=80)
def open_command():
file = tkFileDialog.askopenfile(parent=root,mode='rb',title='Open')
if file != None:
contents = file.read()
textPad.insert('1.0',contents)
file.close()
def save_command(self):
file = tkFileDialog.asksaveasfile(parent=root,mode='w',title='Save')
if file != None:
data = self.textPad.get('1.0', END+'-1c')
file.write(data)
file.close()
def exit_command():
root.destroy()
def copy_command(self, event=None):
self.clipboard_clear()
text = self.get("sel.first", "sel.last")
self.clipboard_append(text)
def cut_command(self, event=None):
self.copy()
self.delete("sel.first", "sel.last")
def paste_command(self, event=None):
text = self.selection_get(selection='CLIPBOARD')
self.insert('insert', text)
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Open...", command=open_command)
filemenu.add_command(label="Save...", command=save_command)
filemenu.add_separator()
filemenu.add_command(label="Copy", command=copy_command)
filemenu.add_command(label="Cut", command=cut_command)
filemenu.add_command(label="Paste", command=paste_command)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=exit_command)
textPad.pack()
root.mainloop()