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)

現在、2つの別々のテーブルを取得していますが、choice_frame'table'は他のテーブルと同じサイズではありません。したがって、このテーブルを最初のテーブルのフレームの一部にしたい(そして、どういうわけかこのセクションだけを削除できるようにしたい)。これは、すでに作業を行っている。もう1つのフレームはframe_table(元のテーブルを作成したフレーム)であり、このフレームと結合したいと考えています。

それ以外の場合は、別のテーブルのように保持したいのですが、[固定]を選択しても表示されなくなります。このコードは、純粋に私が以前に作成したOptionMenuのコマンドです。私が与えられたどんな助けでも大いに感謝します!ありがとうございました。

更新:選択に応じて、行ごとに個別のフレームを取得する必要があります(下の画像を参照)。私はこれに非常に苦労しています!

ここに画像の説明を入力してください

4

1 に答える 1

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

これは、オプションメニューのオプションごとにフレームを作成することで機能します。実際には、これはすべてこのクラスの外部から行うことができます。必要なオプションごとにフレームを追加するだけです。

于 2012-06-25T13:24:31.337 に答える