简体   繁体   中英

Python tkinter add new field by a button

It is python 3.4 here, and the program import tkinter module to build gui interface.

I am wondering how to make a button that is able to add new field for user to fill.
Try to apply built-in method vars() and getattr() but it doesn't work.
I just cannot figure out why AttributeError always pop up.

The following is a part of my code:

import tkinter as Tk
from tkinter import ttk

class dictionary(Tk.Frame):
    def __init__(self, master=None):
        Tk.Frame.__init__(self, master)
        #......other line here........

    def createWidgets(self):
        self.row_c = 0

        self.new_0 = Tk.StringVar(self)
        self.new_0field = ttk.Entry(self, textvariable=self.new_0)
        self.new_0field.grid(row=self.row_c, column=0, columnspan=2, padx=4, pady=6, sticky="NEWS")

        self.add_lang = ttk.Button(self, text="add language", command=self.add_field)
        self.add_lang.bind("<Return>", self.add_field)
        self.add_lang.grid(row=self.row_c, column=3, padx=4, pady=6, sticky="W")
        self.row_c += 1

    def add_field(self):
        #count is a number (string type)
        count = str(self.row_c)
        vars()["self.new_" + count] = Tk.StringVar(self)
        vars()["self.new_" + count + "field"] = ttk.Entry(self, textvariable=getattr(self, "self.new_"+count))
        vars()["self.new_" + count + "field"].grid(row=self.row_c+1, column=0, columnspan=2, padx=4, pady=6, sticky="NEWS")

        self.add_lang.grid(row=self.row_c+1, column=3, padx=4, pady=6, sticky="W")

if __name__ == '__main__':
    root = Tk.Tk()
    app = dictionary(master=root)
    app.mainloop()

Is it possible to add a field by a button in tkinter?
I would be grateful if there is any suggestion to make it work,
Thanks!

So this is my solution:

import tkinter as tk
from tkinter import ttk


class dictionary(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.fields = []
        self.createWidgets()
        self.grid(column=0, sticky="NEWS")

    def createWidgets(self):        
        self.add_field()
        self.add_lang = ttk.Button(self, text="add language", command=self.add_field)
        self.add_lang.bind("<Return>", self.add_field)
        self.add_lang.grid(row=len(self.fields), column=3, padx=4, pady=6, sticky="W")


    def add_field(self):
        self.fields.append({})
        n = len(self.fields)-1
        self.fields[n]['var'] = tk.StringVar(self)
        self.fields[n]['field'] = ttk.Entry(self, textvariable=self.fields[n]['var'])
        self.fields[n]['field'].grid(row=n, column=0, columnspan=2, padx=4, pady=6, sticky="NEWS")
        if n:
            self.add_lang.grid(row=n + 1, column=3, padx=4, pady=6, sticky="W")



if __name__ == '__main__':
    root = tk.Tk()
    app = dictionary(master=root)
    app.mainloop()

I hope this is what you wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM