import tkinter as tk from tkinter import filedialog from common import LayoutCenter class EditForm: def __init__(self, master): self.master = master LayoutCenter(master, "修订评分标准") # 创建滚动条 vsb = tk.Scrollbar(master, orient=tk.VERTICAL) vsb.pack(side="right", fill="y") # 创建文本框并关联滚动条 self.text = tk.Text(self.master, yscrollcommand=vsb.set) self.text.pack(fill="y", expand=True) vsb.config(command=self.text.yview) self.create_menu() self.text.bind("<KeyRelease>", self.update_line_numbers) self.text.tag_configure("line_numbers", justify="right") # 添加行号标记 def update_line_numbers(self,event=None): self.text.delete("line_numbers") for i, line in enumerate(self.text.get("1.0", "end").split("\n")): self.text.insert(f"{i+1}.0", f"{i+1}\n", "line_numbers") def create_menu(self): menu = tk.Menu(self.master) self.master.config(menu=menu) file_menu = tk.Menu(menu) menu.add_cascade(label="File", menu=file_menu) file_menu.add_command(label="Open", command=self.open_file) file_menu.add_command(label="Save", command=self.save_file) file_menu.add_separator() file_menu.add_command(label="Exit", command=self.exit) def open_file(self): file_path = filedialog.askopenfilename() if file_path: with open(file_path, "r") as file: self.text.delete("1.0", "end") self.text.insert("1.0", file.read()) def save_file(self): file_path = filedialog.asksaveasfilename() if file_path: with open(file_path, "w") as file: file.write(self.text.get("1.0", "end")) def exit(self): self.master.destroy() def run(self): self.master.mainloop() root = tk.Tk() my_gui = EditForm(root) root.mainloop()
时间: 2024-03-13 16:43:51 浏览: 185
这段代码中存在语法错误。第一行的import语句和第二行的from语句之间需要添加一个换行符,否则会报错。将这两行代码修改为如下形式即可:
```
import tkinter as tk
from tkinter import filedialog
from common import LayoutCenter
class EditForm:
def __init__(self, master):
self.master = master
LayoutCenter(master, "修订评分标准")
# 创建滚动条
vsb = tk.Scrollbar(master, orient=tk.VERTICAL)
vsb.pack(side="right", fill="y")
# 创建文本框并关联滚动条
self.text = tk.Text(self.master, yscrollcommand=vsb.set)
self.text.pack(fill="y", expand=True)
vsb.config(command=self.text.yview)
self.create_menu()
self.text.bind("<KeyRelease>", self.update_line_numbers)
self.text.tag_configure("line_numbers", justify="right")
# 添加行号标记
def update_line_numbers(self,event=None):
self.text.delete("line_numbers")
for i, line in enumerate(self.text.get("1.0", "end").split("\n")):
self.text.insert(f"{i+1}.0", f"{i+1}\n", "line_numbers")
def create_menu(self):
menu = tk.Menu(self.master)
self.master.config(menu=menu)
file_menu = tk.Menu(menu)
menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Open", command=self.open_file)
file_menu.add_command(label="Save", command=self.save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=self.exit)
def open_file(self):
file_path = filedialog.askopenfilename()
if file_path:
with open(file_path, "r") as file:
self.text.delete("1.0", "end")
self.text.insert("1.0", file.read())
def save_file(self):
file_path = filedialog.asksaveasfilename()
if file_path:
with open(file_path, "w") as file:
file.write(self.text.get("1.0", "end"))
def exit(self):
self.master.destroy()
def run(self):
self.master.mainloop()
root = tk.Tk()
my_gui = EditForm(root)
my_gui.run()
```
请注意,我还将root.mainloop()替换为my_gui.run(),这样可以避免在运行时出现多个Tkinter窗口的问题。
阅读全文