Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating new entry boxes with button Tkinter

Tags:

python

tkinter

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 = Tkinter.Frame()
    labelframe.bind("<Add Input>", callback)
    labelframe.pack()

labelframe = Tkinter.Frame()

labelFrom = Tkinter.Label(labelframe, text= "from")
labelFrom.grid(column=1, row=0)
e = Tkinter.Entry(labelframe)
e.grid(column=1, row=1)

labelTo = Tkinter.Label(labelframe, text= "to")
labelTo.grid(column=2, row=0)
e2 = Tkinter.Entry(labelframe)
e2.grid(column=2, row=1)

labelframe.pack()

addboxButton = Button( root,text='<Add Time Input>', fg="Red",command="addBox")
addboxButton.pack(side=Tkinter.TOP)
like image 619
user3817491 Avatar asked Sep 30 '22 07:09

user3817491


1 Answers

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

like image 67
furas Avatar answered Oct 16 '22 12:10

furas