GUI实例:利用Tkinter实现简单文本编辑器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 from tkinter import * 2 from tkinter.scrolledtext import ScrolledText 3 4 def load(): 5 with open(filename.get()) as file: 6 contents.delete('1.0', END) 7 contents.insert(INSERT, file.read()) 8 9 def save(): 10 with open(filename.get(), 'w') as file: 11 file.write(contents.get('1.0', END)) 12 13 top = Tk() 14 top.title("Simple Editor") 15 16 contents = ScrolledText() 17 contents.pack(side=BOTTOM, expand=True, fill=BOTH) 18 19 filename = Entry() 20 filename.pack(side=LEFT, expand=True, fill=X) 21 22 Button(text='Open', command=load).pack(side=LEFT) 23 Button(text='Save', command=save).pack(side=LEFT) 24 25 mainloop()