Tkinter添加标签帧

时间:2014-08-18 14:23:55

标签: python python-2.7 tkinter

我一直在尝试,但没有做到以下。 我有一个带有一些输入框的标签框架。我想添加一个按钮,以便用户可以在工具中添加多个框架以及现有标签框架下方的输入框和按钮。当添加新框架时,下面的文本框应该被移动。我的tkinter代码如下:

from Tkinter import *
root=Tk()
root.title("CIME")
step = LabelFrame(root,text="Enter Details:")
step.grid(row=0, columnspan=7, sticky='W',padx=5, pady=5, ipadx=5, ipady=5)
Label(step,text="Competitors",font = "Arial 8 bold italic").grid(row=0,sticky='E', padx=5, pady=2)
Label(step,text="Keywords",font = "Arial 8 bold italic").grid(row=1,sticky='E', padx=5, pady=2)
Label(step,text="Project Name",font = "Arial 8 bold italic").grid(row=2,sticky='E', padx=5, pady=2)
e1 = Entry(step)
e2=Entry(step)
e3=Entry(step)
e1.grid(row=0,column=1,columnspan=7, sticky="WE", pady=3,padx=5)
e2.grid(row=1,column=1,columnspan=7, sticky="WE", pady=3,padx=5)
e3.grid(row=2,column=1,columnspan=7, sticky="WE", pady=3,padx=5)
tex = Text(master=root)
scr=Scrollbar(root,orient =VERTICAL,command=tex.yview)
scr.grid(padx=1, column=7, rowspan=15, columnspan=1, sticky=NS)
tex.grid(row = 4,column=1)
tex.config(yscrollcommand=scr.set,font=('Arial', 8, 'bold', 'italic'))
#tex['yscrollcommand'] = sb.set
Button(step,text ="Search Words",width=10,font = "Arial 8 bold    italic",activebackground="red",command=roll).grid(row=3,column=0,sticky=W,pady=4,padx=5)
Button(step,text="Google Search",width=10,font = "Arial 8 bold italic",command = links).grid(row=3,column=2,sticky=W,pady=4,padx=5)
Button(step,text="Extraxt Text",width=10,font = "Arial 8 bold italic",command = create).grid(row=3,column=4,sticky=W,pady=4,padx=5)
mainloop()

enter image description here

2 个答案:

答案 0 :(得分:2)

解决方案的第一部分是创建一个函数来创建labelframe和entry小部件。更好的方法是将该代码移动到一个类中,这样您就可以将整个事件视为单个窗口小部件。这将使您的代码更易于理解,并且更容易在视觉上进行布局。

例如:

class CustomFrame(LabelFrame):
    def __init__(self, parent, text):
        LabelFrame.__init__(self, parent, text=text)

        Label(self,text="Competitors",font = "Arial 8 bold italic").grid(row=0,sticky='E', padx=5, pady=2)
        Label(self,text="Keywords",font = "Arial 8 bold italic").grid(row=1,sticky='E', padx=5, pady=2)
        Label(self,text="Project Name",font = "Arial 8 bold italic").grid(row=2,sticky='E', padx=5, pady=2)
        self.e1=Entry(self)
        self.e2=Entry(self)
        self.e3=Entry(self)
        self.e1.grid(row=0,column=1,columnspan=7, sticky="WE", pady=3,padx=5)
        self.e2.grid(row=1,column=1,columnspan=7, sticky="WE", pady=3,padx=5)
        self.e3.grid(row=2,column=1,columnspan=7, sticky="WE", pady=3,padx=5)

        Button(self,text ="Search Words",width=10,font = "Arial 8 bold italic", activebackground="red",
               command=roll).grid(row=3,column=0,sticky=W,pady=4,padx=5)
        Button(self,text="Google Search",width=10,font = "Arial 8 bold italic",
               command = links).grid(row=3,column=2,sticky=W,pady=4,padx=5)
        Button(self,text="Extraxt Text",width=10,font = "Arial 8 bold italic",
               command = create).grid(row=3,column=4,sticky=W,pady=4,padx=5)

    def getValues(self):
        """Return a dictionary of values from the widget"""
        return {"competitors": self.e1.get(),
                "keywords": self.e2.get(),
                "name": self.e3.get()
        }

接下来,您可以使用该功能创建初始小部件集。此外,由于您要添加新的标签框架,因此最好使用pack来管理这些框架,这样您就不必继续向网格添加行。因此,我建议您为labelframes创建一个容器。然后,您可以使用pack将它们堆叠在另一个之上。

您的主程序看起来像这样:

root = Tk()
root.title("CIME")

frames = Frame(root)
frames.grid(row=0, column=0, sticky="nsew")
tex = Text(master=root)
scr=Scrollbar(root,orient =VERTICAL,command=tex.yview)
scr.grid(row = 1, column=1, padx=1, sticky=NS)
tex.grid(row = 1, column=0)
tex.config(yscrollcommand=scr.set,font=('Arial', 8, 'bold', 'italic'))

step = CustomFrame(frames, "Enter Details:")
step.pack(side="top")

mainloop()

现在,只需要在每次需要另一帧时创建新实例。如果您保留对窗口小部件的引用,则可以使用getValues方法返回包含每个输入窗口小部件值的字典。

现在你需要一个非常简单的函数来在CustomFrame内创建packframes的新实例。它看起来像这样:

def newFrame():
    f = CustomFrame(frames, "Another Frame")
    f.pack(side="top")

...但是,您可能希望将f附加到列表或字典中,以便您可以在代码中的稍后位置获取值。

答案 1 :(得分:0)

定义一个生成输入细节标签框架的功能。并在新按钮的回调函数中调用它。

from Tkinter import *
def roll(): pass
def links(): pass
def create(): pass
root=Tk()
root.title("CIME")

rows = 0
def create_detail_frame():
    global rows
    step = LabelFrame(root,text="Enter Details:")
    step.grid(row=rows, columnspan=7, sticky='W',padx=5, pady=5, ipadx=5, ipady=5)
    Label(step,text="Competitors",font = "Arial 8 bold italic").grid(row=0,sticky='E', padx=5, pady=2)
    Label(step,text="Keywords",font = "Arial 8 bold italic").grid(row=1,sticky='E', padx=5, pady=2)
    Label(step,text="Project Name",font = "Arial 8 bold italic").grid(row=2,sticky='E', padx=5, pady=2)
    e1 = Entry(step)
    e2 = Entry(step)
    e3 = Entry(step)
    e1.grid(row=0,column=1,columnspan=7, sticky="WE", pady=3,padx=5)
    e2.grid(row=1,column=1,columnspan=7, sticky="WE", pady=3,padx=5)
    e3.grid(row=2,column=1,columnspan=7, sticky="WE", pady=3,padx=5)
    Button(step,text ="Search Words",width=10,font = "Arial 8 bold    italic",activebackground="red",command=roll).grid(row=3,column=0,sticky=W,pady=4,padx=5)
    Button(step,text="Google Search",width=10,font = "Arial 8 bold italic",command=links).grid(row=3,column=2,sticky=W,pady=4,padx=5)
    Button(step,text="Extraxt Text",width=10,font = "Arial 8 bold italic",command = create).grid(row=3,column=4,sticky=W,pady=4,padx=5)
    rows += 1

    # Reposition text, scroll
    #scr.grid_forget()
    #tex.grid_forget()
    #scr.grid(row=rows, padx=1, column=7, rowspan=15, columnspan=1, sticky=NS)
    #tex.grid(row=rows,column=1)

    # Reposition text, scroll
    scr.grid(row=rows)
    tex.grid(row=rows)

tex = Text(master=root)
scr=Scrollbar(root,orient =VERTICAL,command=tex.yview)
scr.grid(row=1, padx=1, column=7, rowspan=15, columnspan=1, sticky=NS)
tex.grid(row=1,column=1)
tex.config(yscrollcommand=scr.set,font=('Arial', 8, 'bold', 'italic'))

Button(root, text='Add detail frame', command=create_detail_frame).grid(row=0, column=7)
create_detail_frame()

mainloop()

注意:我使用全局变量rows来跟踪文本小部件上方有多少个网格行。