Python Tkinter中的结束标记

Python Tkinter中的结束标记,python,tkinter,Python,Tkinter,我会直接开始的。 我想用PythonTkinter为HTML、CSS和JS编写一个代码编辑器,但我不知道如何创建一个自动化的“关闭标记系统”。 我还想知道如何让每个标签自动地变成另一种颜色。 我真的很挣扎,因为我也是python新手 下面是一些代码: PYTHON: -Reddroseee这里有一种方法。以字典的形式创建标记,并检查文本小部件中最后插入的字符。如果最后一个字符与字典中的键匹配,则在末尾插入值。还使用以下函数绑定文本小部件: tags = {'{':'}', '[':']', '(

我会直接开始的。 我想用PythonTkinter为HTML、CSS和JS编写一个代码编辑器,但我不知道如何创建一个自动化的“关闭标记系统”。 我还想知道如何让每个标签自动地变成另一种颜色。 我真的很挣扎,因为我也是python新手 下面是一些代码: PYTHON:


-Reddroseee

这里有一种方法。以字典的形式创建标记,并检查文本小部件中最后插入的字符。如果最后一个字符与字典中的键匹配,则在末尾插入值。还使用以下函数绑定文本小部件:

tags = {'{':'}', '[':']', '(': ')'}# add your auto complete tags here
    
    
def autoComplete(*event):
    pos = my_text.index(INSERT)
    text.set(my_text.get('1.0', END))
    inputValue = text.get()[-2:].strip()
    backspace = str(event[0])

    if 'BackSpace' not in backspace.split()[3]:
        if inputValue in list(tags.keys()):
            my_text.insert(END, tags[inputValue])
完整代码:

from tkinter import *
from tkinter import filedialog
from tkinter import font


tags = {'{':'}', '[':']', '(': ')'}# add your auto complete tags here


def autoComplete(*event):
    pos = my_text.index(INSERT)
    text.set(my_text.get('1.0', END))
    inputValue = text.get()[-2:].strip()
    backspace = str(event[0])

    if 'BackSpace' not in backspace.split()[3]:
        if inputValue in list(tags.keys()):
            my_text.insert(END, tags[inputValue])

            
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)

text = StringVar()
#text.trace('w', autoComplete)

#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()

my_text.bind('<KeyRelease>', autoComplete)

#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()
从tkinter导入*
从tkinter导入文件对话框
从tkinter导入字体
标签={{':'}',[':']','(':')'}#在这里添加自动完成的标签
def自动完成(*事件):
pos=我的文字索引(插入)
text.set(my_text.get('1.0',END))
inputValue=text.get()[-2:].strip()
backspace=str(事件[0])
如果“BackSpace”不在BackSpace.split()中[3]:
如果列表中的inputValue(tags.keys()):
my_text.insert(结束,标记[inputValue])
root=Tk()
root.title('text-editor-reddrosee')
根几何体('1200x660')
root.configure(background='white')
#保存文件
def new_文件():
我的文字。删除(“1.0”,结束)
title('新文件-reddrosee')
#打开文件
def open_文件():
我的文字。删除(“1.0”,结束)
text_file=filedialog.askopenfilename(initialdir=“C:/reddrosee/”,title=“Open file”,filetypes=((“HTML文件”,“*.HTML”),(“text文件”,“*.txt”),(“Python文件”,“*.py”),(“所有文件”,“*.*))
名称=文本文件
root.title(name+'-reddrosee')
text\u file=open(text\u file'r')
read\u text=text\u file.read()
我的文本。插入(结束,读取文本)
text_file.close()
def将_另存为_文件():
text_file=filedialog.asksaveasfilename(defaultextension=“*”,initialdir=“C:/reddrosee/”,title=“Save file”,filetypes=(“HTML文件”,“*.HTML”),(“文本文件”,“*.txt”),(“Python文件”,“*.py”),(“所有文件”,“*.HTML”))
如果文本文件:
名称=文本文件
名称=名称。替换(“C:/reddrosee/”,“”)
root.title(name+'-reddrosee')
text\u file=open(text\u file'w')
text_file.write(my_text.get(1.0,END))
root.title('SAVED'+'-reddrosee')
text_file.close()
#创建主框架
我的框架=框架(根)
我的框架包(pady=5)
#滚动条(文本框)
text\u scroll=滚动条(我的滚动框)
text_scroll.pack(侧=右,填充=Y)
text=StringVar()
#text.trace('w',自动完成)
#创建文本框
myFont=font.font(family='monospace')
我的文本=文本(我的框架,宽度=1920,高度=1080,字体=(我的字体,16),bg=“白色”,
选择background=“silver”,fg=“black”,选择foreground=“white”,undo=True,
yscrollcommand=text_scroll.set)
my_text.pack()
my_text.bind(“”,自动完成)
#配置滚动条
text\u scroll.config(命令=my\u text.yview)
#菜单
我的菜单=菜单(根)
root.config(菜单=我的菜单)
#文件菜单
文件菜单=菜单(我的菜单,tearoff=False)
我的菜单。添加级联(label=“File”,menu=文件菜单)
文件菜单。添加命令(label=“New”,command=New\u文件)
文件菜单。添加命令(label=“Open”,command=打开文件)
文件菜单。添加命令(label=“Save”)
文件菜单。添加命令(label=“另存为”,command=另存为文件)
文件菜单。添加分隔符()
文件菜单。添加命令(label=“Exit”,command=root.quit)
#添加编辑菜单
编辑菜单=菜单(我的菜单,tearoff=False)
我的菜单。添加级联(label=“Edit”,menu=编辑菜单)
编辑菜单。添加命令(label=“Cut”)
编辑菜单。添加命令(label=“Copy”)
编辑菜单。添加命令(label=“Undo”)
编辑菜单。添加命令(label=“Redo”)
root.mainloop()

编辑:您也可以在自动完成功能中使用
事件.keycode
而不是切片和使用
strip
。请参阅我的第二个答案。

好的,我正在添加另一个答案。我的前一个答案仅适用于末尾的标签。 使用此选项,将自动完成程序中任何位置的标记

而且,这比前一个好

from tkinter import *
from tkinter import filedialog
from tkinter import font


tags = {'{':'}', '[':']', '(':')'}# add your auto complete tags here
reject_keys = [8, 16, 17, 18, 27, 37, 38, 39, 40]

def autoComplete(event):
 
    pos = f'{float(my_text.index(INSERT))-0.1:1.1f}' 
    inputValue = my_text.get(pos).strip()
    backspace = event.keycode
 
    if backspace not in reject_keys:
        if inputValue in list(tags.keys()):
            insert_pos = my_text.index(INSERT)
            my_text.insert(insert_pos, tags[inputValue])

            
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)

text = StringVar()
#text.trace('w', autoComplete)

#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()

my_text.bind('<KeyRelease>', autoComplete)

#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()
从tkinter导入*
从tkinter导入文件对话框
从tkinter导入字体
标签={{':'}',[':']','(':')'}#在这里添加自动完成的标签
拒绝键=[8,16,17,18,27,37,38,39,40]
def自动完成(事件):
pos=f'{float(my_text.index(INSERT))-0.1:1.1f}'
inputValue=my_text.get(pos).strip()
退格=event.keycode
如果退格不在拒绝键中:
如果列表中的inputValue(tags.keys()):
插入位置=我的文本索引(插入)
my_text.insert(插入位置,标记[inputValue])
root=Tk()
root.title('text-editor-reddrosee')
根几何体('1200x660')
root.configure(background='white')
#保存文件
def new_文件():
我的文字。删除(“1.0”,结束)
title('新文件-reddrosee')
#打开文件
def open_文件():
我的文字。删除(“1.0”,结束)
text_file=filedialog.askopenfilename(initialdir=“C:/reddrosee/”,title=“Open file”,filetypes=((“HTML文件”,“*.HTML”),(“text文件”,“*.txt”),(“Python文件”,“*.py”),(“所有文件”,“*.*))
名称=文本文件
root.title(name+'-reddrosee')
text\u file=open(text\u file'r')
read\u text=text\u file.read()
我的文本。插入(结束,读取文本)
text_file.close()
def将_另存为_文件():
text_file=filedialog.asksaveasfilename(defaultextension=“*”,initialdir=“C:/reddrosee/”,title=“Save file”,filetypes=(“HTML文件”,“*.HTML”),(“文本文件”,“*.txt”),(“Python文件”,“*.py”),(“所有文件”,“*.HTML”))
如果文本文件:
名称=文本文件
名称=名称。替换(“C:/reddrosee/”,“”)
root.title(name+'-reddrosee')
text\u file=open(text\u file'w')
text_file.write(my_text.get(1.0,END))
root.title('SAVED'+'-reddrosee')
text_file.close()
#创建主框架
我的框架=框架(根)
我的框架包(pady=5)
#滚动条(文本框)
text\u scroll=滚动条(我的滚动框)
text_scroll.pack(侧=右,填充=Y)
text=StringVar()
#text.trace('w',自动完成)
#克雷亚
from tkinter import *
from tkinter import filedialog
from tkinter import font


tags = {'{':'}', '[':']', '(':')'}# add your auto complete tags here
reject_keys = [8, 16, 17, 18, 27, 37, 38, 39, 40]

def autoComplete(event):
 
    pos = f'{float(my_text.index(INSERT))-0.1:1.1f}' 
    inputValue = my_text.get(pos).strip()
    backspace = event.keycode
 
    if backspace not in reject_keys:
        if inputValue in list(tags.keys()):
            insert_pos = my_text.index(INSERT)
            my_text.insert(insert_pos, tags[inputValue])

            
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)

text = StringVar()
#text.trace('w', autoComplete)

#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()

my_text.bind('<KeyRelease>', autoComplete)

#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()