等大小的“表格”框架并删除单个框架

时间:2012-06-25 12:10:34

标签: python tkinter

我在Tkinter中创建了一个'table',但是作为一个链接到OptionMenu的独立函数,我创建了另一个框架,需要根据选择添加/删除。我的代码如下:

def ChoiceBox(choice):

    choice_frame = Frame(win1, bg='black')
    choice_frame.grid(row=2, column=1, sticky="ew", padx=1, pady=1)
    column = 0
    if choice == "Fixed":
        choice_frame.grid_forget()      
        tkMessageBox.showinfo("Message", "Fixed.")
    elif choice == "List":
        i = [0, 1, 2, 3]
        for i in i:
            choice_title = Label(choice_frame, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=0)
            choice_title.grid(row=0, column=column+i, sticky="nsew", padx=1, pady=1)

            box = Entry(choice_frame, bg='white', borderwidth=0, width=0)
            box.grid(row=1, column=column+i, sticky="ew", padx=1, pady=1)
    elif choice == "Between" or "Bigger":
        i = [0, 1]
    choice_title1 = Label(choice_frame, text='Min Value', bg='white', borderwidth=0, width=0)
        choice_title1.grid(row=0, column=column, sticky="nsew", padx=1, pady=1)
        choice_title2 = Label(choice_frame, text='Max Value', bg='white', borderwidth=0, width=0)
        choice_title2.grid(row=0, column=column+1, sticky="nsew", padx=1, pady=1)
        for i in i:
            box = Entry(choice_frame, bg='white', borderwidth=0, width=0)
            box.grid(row=1, column=column+i, sticky="nsew", padx=1, pady=1)

我目前正在获取两个单独的表,但是choice_frame'表'与另一个表的大小不同。因此,我希望将此表作为第一个表的框架的一部分(然后以某种方式能够删除此部分),我已经完成了工作。另一个框架是frame_table(我制作原始表格的那个框架),并希望加入这个框架。

否则我希望保持它像一个单独的表,但我不能让它在选择'固定'时消失。这段代码纯粹是我之前创建的OptionMenu的命令。任何帮助,我将不胜感激!谢谢。

更新:现在需要根据选择获得每行的单独帧(参见下图)。我在这方面苦苦挣扎!

enter image description here

1 个答案:

答案 0 :(得分:0)

这是一个稍微好一点的例子(与我之前的例子相比):

import Tkinter as tk

class TableWithFrame(tk.Frame):
    def __init__(self,master=None,*args,**kwargs):
        tk.Frame.__init__(self,master,*args,**kwargs)
        self.table=tk.Frame(self)
        self.table.grid(row=0,column=0)

        self.optionmenu=None
        self.option=tk.StringVar()

        self.frames={}
        self.options=[]
        self.current_choice=None

        self.add_optionmenu()
        self.option.trace("w",self.option_changed)


    def add_option(self,option_string,frame):
        self.options.append(option_string)
        if(self.optionmenu is not None):
            self.optionmenu.destroy()
        self.add_optionmenu()
        self.frames[option_string]=frame

    def add_optionmenu(self):
        if(self.optionmenu is not None):
            self.optionmenu.destroy()

        if(self.options):
            self.optionmenu=tk.OptionMenu(self,self.option,*self.options)
            self.optionmenu.grid(row=1,column=0)

    def option_changed(self,*args):
        choice=self.option.get()
        if(self.current_choice is not None):
            self.current_choice.grid_forget()
        self.current_choice=self.frames[choice]
        self.current_choice.grid(row=0,column=1)


if __name__ == "__main__":
    def populate_table(table):
        """ junk data """
        for j in range(3):
            for i in range(10):
                l=tk.Label(table,text='%d'%(i*j))
                l.grid(row=j,column=i)


    def create_opt_frames(TWF):
        #Fixed Frame
        F=tk.Frame(TWF)
        F.boxes={}
        TWF.add_option('Fixed',F)

        #List Frame
        F=tk.Frame(TWF)
        F.boxes={}
        for i in (1,2,3,4):
            choice_title = tk.Label(F, text='Value %g'% float(i+1))
            choice_title.grid(row=0, column=i, sticky="news")
            box = tk.Entry(F, bg='white')
            box.grid(row=1, column=i, sticky="ew")
            F.boxes[i]=box

        TWF.add_option('List',F)

        #Bigger and Between Frame
        F=tk.Frame(TWF)
        F.boxes={}
        choice_title1 = tk.Label(F, text='Min Value')
        choice_title1.grid(row=0, column=0, sticky="news")
        choice_title2 = tk.Label(F, text='Max Value')
        choice_title2.grid(row=0, column=1, sticky="news")
        for i in (1,2):
            box = tk.Entry(F)
            box.grid(row=1, column=i-1, sticky="nsew", padx=1, pady=1)
            F.boxes[i]=box

        TWF.add_option('Between',F)
        TWF.add_option('Bigger',F)

    def print_boxes(TWF):
        """ Example of how to get info in Entry fields """
        if(TWF.current_choice is not None):
            for k,v in TWF.current_choice.boxes.items():
                print ("boxnum (%d) : value (%s)"%(k,v.get()))
        TWF.after(1000,lambda : print_boxes(TWF))

    root=tk.Tk()
    App=TableWithFrame(root)
    populate_table(App.table)
    create_opt_frames(App)
    print_boxes(App)
    App.grid(row=0,column=0)
    root.mainloop()

这可以通过为选项菜单中的每个选项创建一个框架来实现。实际上,这一切都可以在本课程之外完成。您所做的只是为您想要的每个选项添加一个框架。