Creating new entry boxes with button Tkinter

前端 未结 2 971
一生所求
一生所求 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 10:48

    First of all, the indentation is a whole mess, so I don't know where does the addBox function end ..

    Second, I don't think you need a button, I suppose a checkbutton will do the same thing and it's also more common and familiar to users, I once had this problem and I simply created an entry box and put above it a label indicating that it's optional, and as for code, I simply ignored it if it was empty and verified the input if I found any input ..
    Howerver, that was for only one entry box, and you probably will need something more complex ..

    See this ..

    class OptionsView(Frame):
        """Frame for options in main window"""
        def __init__(self, x, y, parent):
            Frame.__init__(self, parent)
            self.x = x
            self.y = y
            self.placed = False
            self.hidden = False
            self.btn = Button(self, text = 'Button attached to the frame ..', command = lambda: print('Button in frame clicked ..')).pack()
        def pack(self):
            self.place(x = self.x, y = self.y)
            self.placed = True
        def toggle_view(self):
            if self.hidden:
                self.pack()
                self.hidden = False
            else:
                self.place_forget()
                self.hidden = True
    
    if __name__ == '__main__':
        def m_frame():
            if val.get() and not options_frame.placed:
                print('Showing Frame ..')
                options_frame.pack()
            else:
                print('Toggling Frame ..')
                options_frame.toggle_view()
    
        root = Tk()
        root.geometry('300x400+500+600')
        root.title('Testing Hiding Frames ..')
        options_frame = OptionsView(200, 300, root)
    
        val = BooleanVar(value = False)
        Checkbutton(text = 'View more Options ..', var = val, command = m_frame).place(x=root.winfo_height()/2, y=root.winfo_width()/2)
    
        try: root.mainloop()
        except e: showerror('Error!', 'It seems there\'s a problem ..', str(e))
    

    Ofcourse you can also modify the length and the x axis of the main window if you want to be more realistic ..

    0 讨论(0)
  • 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='<Add Time Input>', 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='<Add Time Input>', 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='<Add Time Input>', fg="Red", command=addBox)
    addboxButton.pack()
    
    root.mainloop()
    
    #------------------------------------
    

    enter image description here

    0 讨论(0)
提交回复
热议问题