Tkinter使用if循环创建笔记本页面

时间:2018-04-18 09:20:29

标签: python tkinter tkentry

我有一个程序,它读取HTML模板并搜索变量,并为每个变量创建一个文本输入字段。但是我想在Notebook页面中输入这个字段,但是它只是从最后一个变量创建一个Notebook页面。 我该怎么做?

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)

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案。 我刚为每个笔记本页面添加了一个新框架。现在它的工作。

def start_gui(self):
    notebook = ttk.Notebook(padding='10 10 10 10', width=1280, height=720)
    notebook.grid(column=0, row=0, sticky=(tk.N, tk.W, tk.E, tk.S))
    for page in self.variables:
        self.page = page
        for i, needed_var in enumerate(page):
            print('needed var:', needed_var)
            gui = ttk.Frame(notebook)
            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=155, height=20)
            eing.insert(INSERT, needed_var)
            eing.grid(row=(i * 2)+1, column=1, sticky=NW)
            self.progress_var.append([needed_var, eing])
    gui = ttk.Frame(notebook)
    notebook.add(gui, padding=5, text="Save and Exit")
    ttk.Button(gui, text="SAVE", width=15, command=self.save).grid(row=2, column=1)