我将一些键绑定到我的save_file函数,它首先保存,然后给出错误代码“AttributeError:'Event'对象没有属性'get'”。当我点击主菜单中的按钮时,我真的不懂cos,它可以工作。知道我应该做什么吗?
self.text = Text(root, bg="white", wrap='word', undo=True, foreground="black", exportselection="no")
self.text.bind("<Control-Alt-s>", self.save_file_as)
self.text.bind("<Control-s>", self.save_file)
self.text.bind("<Control-o>", self.open_file)
self.text.focus_set()
self.text.pack(fill='both', expand=1)
功能 self.filename ='' self._filetypes = [('Text',' .txt'),('所有文件',''),]
#calls the save function
def saveFile(self):
self.save_file(self.text)
#saving all The files
def save_file(self, texts):
self.text2 = texts
if (self.filename == ''):
self.save_file_as()
else:
f = open(self.filename, 'w')
f.write(self.text2.get('1.0', 'end')) # change every 'self' that refers to the Text, to self.text2
f.close()
tkinter.messagebox.showinfo('FYI', 'File Saved.')
root.title(self.filename)
def save_file_as(self, whatever=None):
self.filename = tkinter.filedialog.asksaveasfilename(defaultextension='.txt', filetypes=self._filetypes)
f = open(self.filename, 'w')
f.write(self.text2.get('1.0', 'end'))
f.close()
tkinter.messagebox.showinfo('FYI', 'File Saved')
答案 0 :(得分:1)
调用绑定函数时,会将事件对象传递给它。你认为你的参数text
实际上就是那个事件。试试这个:
def save_file(self, event):
self.text2 = event.widget
if (self.filename == ''):
self.save_file_as()
else:
f = open(self.filename, 'w')
f.write(self.text2.get('1.0', 'end')) # change every 'self' that refers to the Text, to self.text2
f.close()
tkinter.messagebox.showinfo('FYI', 'File Saved.')
root.title(self.filename)
def save_file_as(self, event=None, whatever=None):
self.filename = tkinter.filedialog.asksaveasfilename(defaultextension='.txt', filetypes=self._filetypes)
f = open(self.filename, 'w')
f.write(self.text2.get('1.0', 'end'))
f.close()
tkinter.messagebox.showinfo('FYI', 'File Saved')