【问题标题】:Tkinter: how to change end index to spacesTkinter:如何将结束索引更改为空格
【发布时间】: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,它有效,但为什么其他语法也被突出显示?,我的意思是“导入”以外的其他词

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    所以,根据我从您的问题中了解到的情况,我已经即兴编写了代码,请检查并判断这是否是您的要求。

    #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
        current_line = text.index(INSERT).split(".")[0]
        text.tag_add("import", f'{current_line}.0', f'{current_line}.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("<space>", check_syntax)
    
    #text.search()
    Window.update()
    
    #Binds
    #text.bind("<Return>", check_syntax)
    text.bind("<Key>", lambda out = "Unsaved": print(out))
    
    #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()
    

    这里我使用text.index(INSERT) 来获取插入光标在文本小部件中的位置。您可以参考此http://effbot.org/tkinterbook/text.htm#:~:text=Indexes%20are%20used%20to%20point,line%2Fcolumn%20(%E2%80%9Cline. 了解更多信息。然后使用f""current_line 变量插入到字符串中,因此,现在索引会根据您所在的行动态更改。 另外,你错误地使用了lambda函数,语法是lambda arguments: expression

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-09
      • 2014-09-26
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多