Creating new entry boxes with button Tkinter

前端 未结 2 994
一生所求
一生所求 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 ..

提交回复
热议问题