使用按钮Tkinter创建新的输入框

时间:2014-07-18 20:54:10

标签: python tkinter

当用户决定投入更多输入时,如何使按钮在下方添加两个框(并排)?

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)

2 个答案:

答案 0 :(得分:2)

这是添加Entry的示例。

您可能会遇到问题,因为您在command=addBox

中使用了引号

因为您必须从条目中获取值,您必须在列表中记住它们 我添加了从条目中打印文本的按钮。

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()

#------------------------------------

编辑:

并排箱子的例子。

我使用新框架使用grid()并排查看条目 这样我就不会在主窗口/框架中混合grid()pack()

我使用len(all_entries)获取下一个免费列的数量。

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


修改

另一个例子:

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

答案 1 :(得分:0)

首先,缩进是一团糟,所以我不知道addBox函数在哪里结束..

其次,我不认为你需要一个按钮,我想一个按钮会做同样的事情,它也更常见和用户熟悉,我曾经有过这个问题,我只是创建了一个输入框并在其上方放置一个标签,表明它是可选的,至于代码,我只是忽略它,如果它是空的并验证输入,如果我找到任何输入..
Howerver,那是为了只有一个输入框,你可能需要更复杂的东西..

看到这个..

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))

当然,如果你想更加真实,你还可以修改主窗口的长度和x轴。