toga实现记事本安装toga模块:pip install toga 示例代码:import toga from toga.constants import * class TextEditor(toga.App): def startup(self): self.main_window = toga.MainWindow(title=self.name) # 创建文本区域 text_area = toga.MultilineTextInput() # 创建保存按钮 save_button = toga.Button('Save', on_press=self.save_file) # 创建容器并添加控件 box = toga.Box(children=[text_area, save_button]) # 添加容器到主窗口 self.main_window.content = box # 显示窗口 self.main_window.show() def save_file(self, widget): # 弹出保存文件对话框 file_path = self.main_window.save_file_dialog() if file_path: # 从文本区域获取文本内容并写入文件 with open(file_path, 'w') as f: f.write(self.main_window.content.children[0].value) def main(): return TextEditor('Text Editor', 'org.beeware.helloworld') if __name__ == '__main__': app = main() app.main_loop() 代码说明:创建了一个名为TextEditor的类,继承自toga.App类,我们的文本编辑器应用使用这个类来启动。在startup()方法中创建主窗口,并添加文本区域和保存按钮。save_file()方法响应保存按钮事件,调用主窗口的保存文件对话框,获取文件路径,然后将文本区域的内容写入文件。main()函数创建一个TextEditor实例。最后,启动事件循环。这样,我们就实现了一个简单的文本编辑器应用。