我有一个程序可以读取 HTML 模板并搜索变量并为每个变量创建一个文本输入字段。但我想要笔记本页面中的这个条目字段,但 atm 它只是从最后一个变量创建一个笔记本页面。我该怎么做呢?
class writer():
def __init__(self):
self.root = Tk()
self.root.filename = filedialog
self.start_folder = filedialog.askopenfilename(initialdir="/", title="Open HTML Template", filetypes=(("HTML", "*.HTML"), ("all files", "*.*")))
self.solved = {}
self.progress_var = []
self.page = []
self.variables = []
self.load_template(self.start_folder)
self.start_gui()
self.writer()
def save(self):
for var, eing in self.progress_var:
self.solved[var] = eing.get('1.0', END)
print('Var:', self.solved)
def load_template(self, start_folder):
with open(start_folder, mode="r") as htmlfile:
for line in htmlfile.readlines():
if '{{' in line and '}}' in line:
found_var = line[line.find('{{')+2:line.find('}}')]
if found_var[:6].lower() == 'header':
if self.page:
self.variables.append(self.page)
self.page = []
self.page.append(found_var)
else:
self.page.append(found_var)
self.variables.append(self.page)
def start_gui(self):
notebook = ttk.Notebook(padding='10 10 10 10', width=600, height=200)
notebook.grid(column=0, row=0, sticky=(tk.N, tk.W, tk.E, tk.S))
gui = ttk.Frame(notebook)
for page in self.variables:
self.page = page
for i, needed_var in enumerate(page):
print('needed var:', needed_var)
# ttk.Button(gui, text=needed_var, width=15, command=self.save).grid(row=2, column=1)
# ttk.Button(gui, text=needed_var, width=15, command=self.save).grid(row=4, column=1)
notebook.add(gui, padding=5, text=needed_var)
field = LabelFrame(gui, text=needed_var[1], padx=100, pady=50)
field.grid(row=i*2, column=1, sticky=NW)
eing = scrolledtext.ScrolledText(gui, width=40)
eing.insert(INSERT, needed_var)
eing.grid(row=(i * 2)+1, column=1, sticky=NW)
self.progress_var.append([needed_var, eing])
ttk.Button(gui, text="SAVE", width=15, command=self.save).grid(row=2, column=1)
gui.mainloop()
def writer(self):
with open(self.start_folder, mode="r") as htmlfile:
with open("writer.html", 'w') as writefile:
for line in htmlfile.readlines():
if '{{' in line and '}}' in line:
found_var = line[line.find('{{') + 2:line.find('}}')]
line = line.replace("{{" + found_var + "}}", self.solved[found_var])
writefile.writelines(line)