我会直接开始的。 我想用PythonTkinter为HTML、CSS和JS编写一个代码编辑器,但我不知道如何创建一个自动化的“关闭标记系统”。 我还想知道如何让每个标签自动地变成另一种颜色。 我真的很挣扎,因为我也是python新手 下面是一些代码: PYTHON:
from tkinter import *
from tkinter import filedialog
from tkinter import font
root = Tk()
root.title('text-editor - reddrosee')
root.geometry('1200x660')
root.configure(background='white')
#SAVEFILE
def new_file():
my_text.delete("1.0", END)
root.title('New File - reddrosee')
#OPEN FILE
def open_file():
my_text.delete("1.0", END)
text_file = filedialog.askopenfilename(initialdir="C:/reddrosee/", title="Open File", filetypes=(("HTML Files", "*.html"), ("Text Files", "*.txt"), ("Python Files", "*.py"), ("All Files", "*.*")))
name = text_file
root.title(name + ' - reddrosee')
text_file = open(text_file, 'r')
read_text = text_file.read()
my_text.insert(END, read_text)
text_file.close()
def save_as_file():
text_file = filedialog.asksaveasfilename(defaultextension=".*", initialdir="C:/reddrosee/", title="Save File", filetypes=(("HTML Files", "*.html"), ("Text Files", "*.txt"), ("Python Files", "*.py"), ("All Files", "*.*")))
if text_file:
name = text_file
name = name.replace("C:/reddrosee/", "")
root.title(name + ' - reddrosee')
text_file = open(text_file, 'w')
text_file.write(my_text.get(1.0, END))
root.title('SAVED' + ' - reddrosee')
text_file.close()
#CREATE MAIN FRAME
my_frame = Frame(root)
my_frame.pack(pady=5)
#SCROLLBAR(TEXTBOX)
text_scroll = Scrollbar(my_frame)
text_scroll.pack(side=RIGHT, fill=Y)
#CREATE TEXTBOX
myFont = font.Font(family='monospace')
my_text = Text(my_frame, width=1920, height=1080, font=(myFont, 16), bg="white",selectbackground="silver", fg="black" ,selectforeground="white", undo=True, yscrollcommand=text_scroll.set)
my_text.pack()
#CONFIG SCROLLBAR
text_scroll.config(command=my_text.yview)
#MENU
my_menu = Menu(root)
root.config(menu=my_menu)
#FILEMENU
file_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save")
file_menu.add_command(label="Save As", command=save_as_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command= root.quit)
#ADD EDIT MENU
edit_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut")
edit_menu.add_command(label="Copy")
edit_menu.add_command(label="Undo")
edit_menu.add_command(label="Redo")
root.mainloop()
-红玫瑰
这里有一种方法。以字典的形式创建标记,并检查文本小部件中最后插入的字符。如果最后一个字符与字典中的键匹配,则在末尾插入值。还使用以下函数绑定文本小部件:
完整代码:
编辑:您也可以在自动完成函数中使用
event.keycode
而不是切片和使用strip
。请参阅我的第二个答案好的,我正在添加另一个答案我之前的答案只适用于结尾的标签。 使用此选项,将自动完成程序中任何位置的标记
而且,这比前一个好
相关问题 更多 >
编程相关推荐