【发布时间】:2020-08-14 05:20:07
【问题描述】:
所以我制作了一个文本编辑器,并通过在我的代码中输入 tag_add() 来修复我之前的问题
CODE
#Importing modules
from tkinter import *
#Main Window
Window = Tk()
Window.geometry("400x550")
Window.minsize(400, 550)
##Main Script
#Defs
def check_syntax(event=None):
#Tag adds
#Import syntax
text.tag_add("import", "1.0", "1.6")
#Tag configures
text.tag_configure("import", foreground="yellow")
"""
def check_syntax(event=None):
offset = '+%dc' % len("import")
pos_start = text.search("import", 1.0, END)
pos_end = pos_start + offset
text.tag_add('import', pos_start, pos_end)
#text.tag_remove("import", 1.0, END)
def del_for_check_syntax(event=None):
text.tag_remove("import", 1.0, END)"""
#Main frame
main = Frame(Window)
#Main text widget
text = Text(main, bd=0, highlightthickness=0, borderwidth=0, bg="#323232", fg="white", font=("Hack Italic", 20), undo=True)
#Menu bar
#Mainmenu
mainmenu = Menu(Window)
#Filemenu
filemenu = Menu(mainmenu, tearoff=0)
#Commands
#Filemenu commands
filemenu.add_command(label="New")
#Configs
text.config(width=55, height=35)
main.config(width=55, height=35)
#Tag config for coloring syntax
#text.tag_configure("import", foreground="yellow")
#Highlighting syntax
text.bind("<Return>", check_syntax)
#text.search()
Window.update()
#Binds
#text.bind("<Return>", check_syntax)
text.bind("<Key>", lambda: print("Unsaved"))
#Packs and places
#main.place(anchor="c", rely=.5, relx=.5)
main.pack(expand=True, fill=BOTH, side="right")
text.pack(expand=True, fill=BOTH)
#Update window
Window.update()
#Window.mainloop()
Window.mainloop()
问题
tkinter 仅在第 1 行突出显示 import 而没有突出显示下一行
问题
有什么方法可以使结束索引为空格,我的意思是结束索引在空格上,所以每次用户在文本小部件中输入import 时,下一行也会突出显示,而不仅仅是 1 行,然后开始索引以新的开始文本小部件中带有 import 的空格
编辑
谢谢@AST,它有效,但为什么其他语法也被突出显示?,我的意思是“导入”以外的其他词
【问题讨论】: