【发布时间】:2021-08-16 19:28:26
【问题描述】:
我目前正在使用 Python tkinter 开发 HTML 编辑器应用程序。当您创建新项目或打开现有文件时,屏幕上会显示一个文本小部件,以及一个运行 HTML 代码的 Run 按钮。当您键入任何 HTML 开始标签时,脚本将立即向该元素添加一个结束标签(自动完成)。我正在使用一个名为get_stringvar 的函数,它受任何密钥释放的约束。该功能负责自动完成。当我键入我的第一个开始标签时,脚本会自动完成结束标签。但是当我输入我的第二个开始标签时,结束标签没有被自动完成。有人可以帮我解决这个问题吗?我的一些代码:
import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
from tkinter import messagebox
import webbrowser
import os
window = tk.Tk()
window.title("HTML Editor")
window.configure(bg="grey")
window.state("zoomed")
title = tk.Label(window, text="HTML Editor", font=("Arial Rounded MT Bold", 40, "underline"), bg="grey")
title.place(x=400, y=20)
def get_stringvar(event):
content = text_box.get("1.0", tk.END)
keys = ["Return", "Up", "Down", "Left", "Right"]
if event.keysym in keys:
return
line = content.splitlines()[-1]
if "/" not in line and line != "<!DOCTYPE html>":
if ("<" in line and ">" in line) and line.index(">") + 1 == len(line):
index = line.index("<")
index2 = line.index(">") + 1
new_line = "<" + "/" + line[index + 1:index2 - 1] + ">"
text_box.insert(tk.END, new_line)
line2 = content.splitlines().index(line) + 1
text_box.mark_set("insert", "%d.%d" % (line2, index2))
create = tk.Button(window, text="Create a new file", width=17, height=3, font=("Arial Rounded MT Bold", 20),
command=save_file)
create.place(x=420, y=200)
open_e = tk.Button(window, text="Open an existing file", width=17, height=3, font=("Arial Rounded MT Bold", 20),
command=open_file)
open_e.place(x=420, y=350)
window.protocol("WM_DELETE_WINDOW", on_closing)
frame = tk.Frame(window, bd=2, relief="raised")
text_box = tk.Text(window, font=("Courier New", 10), fg="black")
text_box.bind("<KeyRelease>", get_stringvar)
scroll_bar = tk.Scrollbar(window, command=text_box.yview)
run_b = tk.Button(frame, text="Run", width=6, height=2, bg="white", command=run_code)
text_box.configure(yscrollcommand=scroll_bar.set)
window.mainloop()
更新代码(非常感谢@TheLizzard):
import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
from tkinter import messagebox
import webbrowser
import os
window = tk.Tk()
window.title("HTML Editor")
window.configure(bg="grey")
window.state("zoomed")
title = tk.Label(window, text="HTML Editor", font=("Arial Rounded MT Bold", 40, "underline"), bg="grey")
title.place(x=400, y=20)
def get_stringvar(event):
line = text_box.get("insert linestart", "insert")
if (line[-1] == ">") and ("<" in line):
new_line = "</" + line[line.rindex("<") + 1:-1] + ">"
text_box.insert("insert", new_line)
text_box.mark_set("insert", f"insert-{len(new_line)}c")
return "break"
create = tk.Button(window, text="Create a new file", width=17, height=3, font=("Arial Rounded MT Bold", 20),
command=save_file)
create.place(x=420, y=200)
open_e = tk.Button(window, text="Open an existing file", width=17, height=3, font=("Arial Rounded MT Bold", 20),
command=open_file)
open_e.place(x=420, y=350)
window.protocol("WM_DELETE_WINDOW", on_closing)
frame = tk.Frame(window, bd=2, relief="raised")
text_box = tk.Text(window, font=("Courier New", 10), fg="black")
text_box.bind("<Tab>", get_stringvar)
scroll_bar = tk.Scrollbar(window, command=text_box.yview)
run_b = tk.Button(frame, text="Run", width=6, height=2, bg="white", command=run_code)
text_box.configure(yscrollcommand=scroll_bar.set)
window.mainloop()
【问题讨论】:
-
你能做一个最小的工作示例吗?现在我们真的不需要 1/2 的代码,就像所有文件保存/打开的东西一样。
-
@TheLizzard,我会编辑一些没有问题的代码
-
尝试绑定到
"<KeyRelease>"而不是"<KeyPress>" -
@TheLizzard,还是不行
-
@TheLizzard,现在它可以工作了,即使我没有改变任何东西。