为作业设计文本编辑器,并不断获得 ' NameError:全球名称'事件'没有定义' 这个python编程的新手,所以任何帮助都会很棒。我已经删除了一些代码作为脚本很长时间,我认为包含了重要的部分。
from tkinter import *
from tkinter.ttk import *
from tkinter.scrolledtext import ScrolledText
import tkinter.filedialog
from datetime import datetime, date
import tkinter.messagebox as box
import tkinter.font
class Notepad(Frame):
# initialization. receives the master widget
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.parent.attributes('-topmost', False)
self.GUI()
def GUI(self):
# Main window and title
self.parent.title("Assisgnments")
self.pack(fill=BOTH, expand=True)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3,weight=1)
self.rowconfigure(5, pad=7)
# Menu bar and drop down ADD
menu = Menu(self.parent)
self.parent.config(menu=menu)
edit = Menu(self.parent, tearoff=0)
edit.add_command(label="Image", command=self.showImg)
edit.add_command(label="Text", command=lambda:self.showTxt(self)) #
menu.add_cascade(label="Edit", menu=edit)
self.popup_menu = Menu(self.parent, tearoff=0)
self.popup_menu.add_command(label="link")
# Text area and buttons created, positioned and labeled
self.label = Label(self, text="Notes", font="Bold")
self.label.grid(sticky=W, pady=4, padx=5)
self.text = ScrolledText(self)
self.text.grid(row=1, column=0, columnspan=2, rowspan=6, padx=5, sticky=W+S+N)
cbtn = Button(self, text="Save", command=lambda:files.save_text(self))
cbtn.grid(row=10, column=4, pady=4)
hbtn = Button(self, text="Open",command=lambda:files.load_file(self), width=10)
hbtn.grid(row=10, column=0, padx=5)
self.obtn = Button(self, text="Quit", underline=0, command=self.parent.destroy)
self.obtn.grid(row=10, column=5, pady=(2,4))
self.bind("<Button-3>", self.do_popup())
def do_popup(self):
self.popup_menu.post(event.x_root, event.y_root)
def sequence(self,*functions):
def func(*args, **kwargs):
return_value = None
for function in functions:
return_value = function(*args, **kwargs)
return return_value
return func
class files(Notepad):
def load_file(self):
self.file_name = tkinter.filedialog.askopenfilename() #("ProgNotes.txt"),("WebAppNotes.txt")
infile = open(self.file_name,"r")
self.text.insert(END, infile.read())
infile.close()
def note_text(self, name):
self.file = open("%sNotes.txt"%name, "r")
self.text.delete("1.0",END)
self.text.insert(END, self.file.read())
def save_text(self):
self.file_name = tkinter.filedialog.asksaveasfilename()
outfile = open("%s"%self.file_name, "w")
outfile.write(self.text.get("1.0", END))
#outfile = self.text.get(1.0, END)
def save_notes(self, name):
with open("%sNotes.txt"%name, "w") as self.file:
self.file.write(self.text.get("1.0", END))
self.file.close()
# New window instance with assignment info
def main():
root = Tk()
file_name = None
root.geometry("725x600+300+300")
Notepad(root)
root.mainloop()
if __name__ == '__main__':
main()