我正在编写代码编辑器,并且存在TabError。
from tkinter import *
import os
from tkinter.filedialog import *
from tkinter.messagebox import *
root = Tk("Note")
font_family = "Arial"
font_size = 18
def save_as():
global text
t = text_area.get("1.0", "end-1c")
savelocation=asksaveasfilename()
file1=open(savelocation, "w+")
file1.write(t)
file1.close()
def open():
__file = askopenfilename(defaultextension=".txt",
filetypes = (("All Files","*.*"),
("Text Documents","*.txt"),
("Python","*.py"),
("JavaScript","*.js"),
("HTML","*.html")))
file = open(__file, "r")
text_area.insert(1.0,file.read())
file.close()
text_area = Text(root, font=(font_family + " " + str(font_size) ))
text_area.grid()
#menu
menu = Menu(root)
root.config(menu=menu)
file_menu = Menu(menu, tearoff=0)
menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Save as", command=save_as)
file_menu.add_command(label="Open", command=open)
file_menu.add_separator()
root.mainloop()
答案 0 :(得分:1)
您不能在源代码中将缩进与制表符和空格混合使用。如果您得到一个TabError。
还有另一件事;函数open()
是一个内置函数,当您重新定义它时,原始Python函数将不可用。调用用于打开文件的函数。