用python写用户交互界面,导入文本报错
from tkinter.filedialog import *
from tkinter.colorchooser import *
class Application(Frame):
def init(self, master=None):
super().init(master)
self.master = master
self.textpad = None
self.pack()
self.createWidget()
def createWidget(self):
menubar = Menu(root)
menuFile = Menu(menubar)
menuEdit = Menu(menubar)
menuHelp = Menu(menubar)
menubar.add_cascade(label="文件(F)", menu=menuFile)
menubar.add_cascade(label="编辑(E)", menu=menuEdit)
menubar.add_cascade(label="帮助(H)", menu=menuHelp)
menuFile.add_cascade(label="新建", accelerator="ctrl+n", command=self.newfile)
menuFile.add_cascade(label="打开", accelerator="ctrl+o", command=self.openfile)
menuFile.add_cascade(label="保存", accelerator="ctrl+s", command=self.savefile)
menuFile.add_separator()
menuFile.add_command(label="退出", accelerator="ctrl+q", command=self.exit)
root["menu"] = menubar
root.bind("" , lambda event: self.newfile())
root.bind("" , lambda event: self.openfile())
root.bind("" , lambda event: self.savefile())
root.bind("" , lambda event: self.exit())
self.textpad = Text(root, width=200, height=60)
self.textpad.pack()
self.contextMenu = Menu(root)
self.contextMenu.add_command(label="背景颜色", command=self.openAskColor())
root.bind("Button-3", self.createContextMenu)
self.button1 = Button(self)
self.button2 = Button(self)
self.button3 = Button(self)
self.button4 = Button(self)
self.button5 = Button(self)
self.button6 = Button(self)
self.button1["text"] = "因果关系"
self.button2["text"] = "时序关系"
self.button3["text"] = "条件关系"
self.button4["text"] = "比较关系"
self.button5["text"] = "扩展关系"
self.button6["text"] = "并列关系"
self.button1.pack(side='left')
self.button2.pack(side='left')
self.button3.pack(side='left')
self.button4.pack(side='left')
self.button5.pack(side='left')
self.button6.pack(side='left')
self.button1["command"] = self.reason_result
self.button2["command"] = self.time_sequence
self.button3["command"] = self.condition
self.button4["command"] = self.comparison
self.button5["command"] = self.extension
self.button6["command"] = self.juxtaposition
def reason_result(self):
self.textpad.insert(INSERT, "" )
def time_sequence(self):
self.textpad.insert(INSERT, "" )
def condition(self):
self.textpad.insert(INSERT, "" )
def comparison(self):
self.textpad.insert(INSERT, "" )
def extension(self):
self.textpad.insert(INSERT, "" )
def juxtaposition(self):
self.textpad.insert(INSERT, "" )
def newfile(self):
self.filename = asksaveasfilename(title="另存为", initialfile="未命名.txt",
filetypes=[("文本文档", "*.txt")],
defaultextension=".txt")
self.savefile()
def openfile(self):
with askopenfile(title="打开文本文件") as f:
self.textpad.insert(INSERT, f.read())
self.filename = f.name
def savefile(self):
with open(self.filename, 'w') as f:
c = self.textpad.get(1.0, END)
f.write(c)
def exit(self):
root.quit()
def openAskColor(self):
s1 = askcolor(color="red", title="选择背景色")
self.textpad.config(bg=s1[1])
def createContextMenu(self, event):
self.contextMenu.post(event.x_root, event.y_root)
if name == 'main':
root = Tk()
root.geometry("450x300+200+300")
root.title("篇章关系标注器")
app = Application(master=root)
root.mainloop()
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Program Files (x86)\anaconda\lib\tkinter_init.py", line 1883, in call
return self.func(*args)
File "D:/0.dissertation/1.dissertation汉语隐式篇章关系识别/工具/0.用户界面/语料库标注用户界面/1.py", line 107, in savefile
with open(self.filename, 'w') as f:
AttributeError: 'Application' object has no attribute 'filename'
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Program Files (x86)\anaconda\lib\tkinter_init.py", line 1883, in call
return self.func(*args)
File "D:/0.dissertation/1.dissertation汉语隐式篇章关系识别/工具/0.用户界面/语料库标注用户界面/1.py", line 103, in openfile
self.textpad.insert(INSERT, f.read())
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 32: illegal multibyte sequence
试试这样
with open(askopenfilename(title="打开文件"), encoding='utf-8') as f:
self.textpad.insert(INSERT, f.read())