tkinter新建文档功能
时间: 2023-12-22 10:29:27 浏览: 28
以下是使用Tkinter库制作的新建文档功能的示例代码:
```python
import tkinter as tk
from tkinter import filedialog
def new_document():
# 创建一个新的文档
document = tk.Toplevel(root)
document.title("Untitled")
document.geometry("400x300")
def main():
global root
root = tk.Tk()
root.title("Text Editor")
root.geometry("800x600")
# 创建菜单栏
menubar = tk.Menu(root)
root.config(menu=menubar)
# 创建File菜单
file_menu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=file_menu)
# 添加New功能
file_menu.add_command(label="New", command=new_document)
# 添加Open功能
file_menu.add_command(label="Open", command=open_document)
# 添加Save As功能
file_menu.add_command(label="Save As", command=save_document)
# 添加Exit功能
file_menu.add_command(label="Exit", command=root.quit)
root.mainloop()
if __name__ == "__main__":
main()
```
这段代码创建了一个基本的文本编辑器窗口,其中包含一个File菜单。在File菜单中,点击New功能会弹出一个新的文档窗口,窗口标题为"Untitled"。你可以根据需要进一步完善这个新建文档功能,例如添加保存文档的功能等。