Creating new entry boxes with button Tkinter

前端 未结 2 972
一生所求
一生所求 2021-01-14 10:22

How do i make the button to add two box (side by side) below when it is being clicked as the user decided to put more input?

def addBox():
    labelframe = T         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-14 11:00

    This is example how to add Entry.

    Probably you get problem because you use quotation marks in command=addBox

    Because you will have to get values from entries you have to remeber them on list.
    I add button which print text from entries.

    from Tkinter import *
    
    #------------------------------------
    
    def addBox():
        print "ADD"
    
        ent = Entry(root)
        ent.pack()
    
        all_entries.append( ent )
    
    #------------------------------------
    
    def showEntries():
    
        for number, ent in enumerate(all_entries):
            print number, ent.get()
    
    #------------------------------------
    
    all_entries = []
    
    root = Tk()
    
    showButton = Button(root, text='Show all text', command=showEntries)
    showButton.pack()
    
    addboxButton = Button(root, text='', fg="Red", command=addBox)
    addboxButton.pack()
    
    root.mainloop()
    
    #------------------------------------
    

    EDIT:

    Example with boxes side by side.

    I use new frame to keep entries side by side using grid().
    This way I don't mix grid() with pack() in main window/frame.

    I use len(all_entries) to get number of next free column.

    from Tkinter import *
    
    #------------------------------------
    
    def addBox():
        print "ADD"
    
        # I use len(all_entries) to get nuber of next free column
        next_column = len(all_entries)
    
        # add label in first row 
        lab = Label(frame_for_boxes, text=str(next_column+1))
        lab.grid(row=0, column=next_column)
    
        # add entry in second row
        ent = Entry(frame_for_boxes)
        ent.grid(row=1, column=next_column)
    
        all_entries.append( ent )
    
    #------------------------------------
    
    def showEntries():
    
        for number, ent in enumerate(all_entries):
            print number, ent.get()
    
    #------------------------------------
    
    all_entries = []
    
    root = Tk()
    
    showButton = Button(root, text='Show all text', command=showEntries)
    showButton.pack()
    
    addboxButton = Button(root, text='', fg="Red", command=addBox)
    addboxButton.pack()
    
    frame_for_boxes = Frame(root)
    frame_for_boxes.pack()
    
    root.mainloop()
    
    #------------------------------------
    

    enter image description here


    EDIT:

    Another example:

    from Tkinter import *
    
    #------------------------------------
    
    def addBox():
        print "ADD"
    
        frame = Frame(root)
        frame.pack()
    
        Label(frame, text='From').grid(row=0, column=0)
    
        ent1 = Entry(frame)
        ent1.grid(row=1, column=0)
    
        Label(frame, text='To').grid(row=0, column=1)
    
        ent2 = Entry(frame)
        ent2.grid(row=1, column=1)
    
        all_entries.append( (ent1, ent2) )
    
    #------------------------------------
    
    def showEntries():
    
        for number, (ent1, ent2) in enumerate(all_entries):
            print number, ent1.get(), ent2.get()
    
    #------------------------------------
    
    all_entries = []
    
    root = Tk()
    
    showButton = Button(root, text='Show all text', command=showEntries)
    showButton.pack()
    
    addboxButton = Button(root, text='', fg="Red", command=addBox)
    addboxButton.pack()
    
    root.mainloop()
    
    #------------------------------------
    

    enter image description here

提交回复
热议问题