2012-06-25 33 views
1

我已经在Tkinter中创建了一个“表格”,但作为与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

+0

我很困惑你要在这里做什么。你说的另一个框架是什么('win1'也许?)?每次你调用这个函数,你都在创建一个新的框架。我认为你想持有对创建框架的引用,然后决定是将它放在网格上还是“grid_forget”它(或者可能会销毁旧的“Frame”,然后用新的框架替换它)... – mgilson

+0

另一个框架是我在其中创建表格并且不是根框架。当我想根据选择做几件不同的事情时,我觉得我很困惑。 – user2063

+0

当我按下按钮时,我期待从这些Entry小部件中获取值,但是我正在混合所有的定义。我会如何去做这件事? – user2063

回答

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

这是通过创建在选项菜单中每个选项的框架。实际上,这可以全部在这个班级以外完成。你所做的只是为你想要的每个选项添加一个框架。

+0

我真的很感谢你的帮助和解答。不过,我想知道是否有可能在聊天中向我展示我的整个代码,因为它更像是一个整合的问题......我也没有使用过自己等等。你认为这是可能的吗? – user2063

+0

@ user2063 - 查看更新的代码。 (尤其是'if __name__ =='__main __''中的东西) – mgilson

+0

我已经回过头来回答这个问题,因为我已经在课堂上进一步了解了一些内容。然而,我很难将其他一些定义合并为I不明白如何解决'如果__name__ =='__main__''并且不断收到'自我未定义'的错误。你能解释一下这条线是怎么做的,以及如何在没有这个错误的情况下继续添加我的其他函数?谢谢。 – user2063