Ivar
Ivar

Reputation: 31

Python - Adding new entry box when button is pressed (Tkinter) W/O global code

I know there is a similar question but I'm wondering how I go about doing this without any global code. I want a new entry to pop up (as well as a label next to it) when a button is pressed.

class Options(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent

    def add(self):
        new_entry = Entry(self)
        new_entry.grid()

def main():
    t = Tk()
    frame = Options(t)
    frame.pack()

    b0 = Button(frame, text ="Add entry", command = frame.add())
    b0.grid()

Upvotes: 0

Views: 1935

Answers (1)

Lafexlos
Lafexlos

Reputation: 7735

Your code actually does what you want. The only problem is you are calling the frame.add function instead of passing it as command, in button creation line by adding (). Remove those parenthesis and you will be OK.

b0 = Button(frame, text ="Add entry", command = frame.add) #no parenthesis here

If you want a pop-up, you need to create a Toplevel and put what you want in it.(Entry and Label for your case)

def add(self):
    self.top = Toplevel(self)
    new_entry = Entry(self.top)
    new_entry.grid()

Upvotes: 1

Related Questions