Python Tkinter中的结束标记

2024-10-03 06:21:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我会直接开始的。 我想用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()

-红玫瑰


Tags: textnameaddtitlemyrootfilesedit
2条回答

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

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

编辑:您也可以在自动完成函数中使用event.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()

相关问题 更多 >