0

作成した4つのエントリボックスのエントリを取得するための次のコード(そのセクションのみ)があります。しかし、私には2つの問題があります。

  • 各ボックスに入力すると、同じものが入力されます。異なる番号を入力して、それらすべてを別々の変数に割り当てたいと思います。

  • ループ内に4つのボックスを作成し、これより少ない行を作成する方法はありますか?

    number = StringVar()def numberwritten(* args):number.trace( "w"、numberwritten)fg = number.get()print fg

    別の定義でdefChoiceBox(choice):(このdefの下の完全なコードではありません)

    def ChoiceBox(choice):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)
    
        box1 = Entry(choice_frame, bg='white', borderwidth=0, width=0, textvariable=number)
        box1.grid(row=1, column=0, sticky="ew", padx=1, pady=1)
        box2 = Entry(choice_frame, bg='white', borderwidth=0, width=0, textvariable=number)
        box2.grid(row=1, column=1, sticky="ew", padx=1, pady=1)
        box3 = Entry(choice_frame, bg='white', borderwidth=0, width=0, textvariable=number)
        box3.grid(row=1, column=2, sticky="ew", padx=1, pady=1)
        box4 = Entry(choice_frame, bg='white', borderwidth=0, width=0, textvariable=number)
        box4.grid(row=1, column=3, sticky="ew", padx=1, pady=1)
    

更新/編集:

これは私が持っているコードのセクションであり、構文エラーを受け取っているため、最後に何が問題になっているのか理解できません。

def numberwritten(number):
    fg = number.get()
    print fg

numbers = [StringVar() for i in xrange(4) ] #Name available in global scope. 
for i in numbers: 
    i.trace('w',lambda n=i: numberwritten(n) ) 


def ChoiceBox(choice):


    column = 0
    if choice == "Fixed":
        choice_frame.grid_forget()      
    tkMessageBox.showinfo("Message", "No optimisation, value fixed.")
    elif choice == "List":

        for i in xrange(4): 
            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) 
        boxes=[] 

        tkMessageBox.showinfo("Message", "Please fill in list values.")


    elif choice == "Interval" or "Optimisation":
        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="N S E W", 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)

        boxes=[]

        tkMessageBox.showinfo("Message", "Enter Min/Max values.")

for i in xrange(4): 
        box=Entry(choice_frame,bg='white',borderwidth=0,textvariable=numbers[i]) 
        box.grid(row=1,column=i, sticky='ew', padx=1, pady=1 
        boxes.append(box)
    box1,box2,box3,box4=boxes
4

2 に答える 2

1

は(および Tkinter の他のいくつかのもの)StringVarのモデルです。EntryEntryインスタンスに同じStringVarオブジェクトへの参照を与えたので、自然にモデルが共有され、同じ内容が表示されます。StringVarそれぞれに 1 つずつ、合計 4 つの異なるオブジェクトを作成する必要がありますEntry。(これらは、s のコレクションを反復処理するループで作成しますStringVar…)

于 2012-06-26T14:46:55.187 に答える
0

まず、次の使用を中止してください。

i=[1,2,3,4]
for i in i:
    ...

使用する:

for i in (1,2,3,4):
    ...

代わりは。

またはより良い:

for i in xrange(1,5): #range in python3.x
    ...

さて、ループ内のものを作成するのは簡単です:

strvars=[]
boxes=[]
for i in xrange(1,5):
    svar=StringVar()
    box=Entry(choice_frame, bg='white', borderwidth=0, width=0, textvariable=svar)
    box.grid(row=1, column=i-1, sticky="ew", padx=1, pady=1)
    svar.trace('w',numberwritten)
    #If you want to pass the box associated with the stringvar, you can do this:
    #svar.trace('w',lambda a,b,c,box=box : numberwritten(box)) #box.get() get's the contents of an Entry too!
    strvars.append(svar)
    boxes.append(box)

number1,number2,number3,number4=strvars  #unpack stringvars 
box1,box2,box3,box4=boxes   #unpack boxes

StringVarボックスごとに個別のものが必要です。解凍は、StringVars および Entry ボックスを保持するリストに含まれるアイテムの数がわかっている場合にのみ機能します。それ以外の場合は、たとえばboxes[0]、必要なボックス/変数への参照を取得できます。strvars[0]

編集

このようなものはうまくいくはずです...

def numberwritten(number):
    fg = number.get()
    print fg 

numbers = [StringVar() for i in xrange(4) ]  #Name available in global scope.
for i in numbers:
    i.trace('w',lambda a,b,c,n=i: numberwritten(n) )

# In separate definition def ChoiceBox(choice): (not full code under this def)
def ChoiceBox(choice):  

    for i in xrange(4):
        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)

    boxes=[]
    for i in xrange(4):
        box=Entry(choice_frame,bg='white',borderwidth=0,textvariable=numbers[i])
        box.grid(row=1,column=i, sticky='ew', padx=1, pady=1
        boxes.append(box)

    box1,box2,box3,box4=boxes

余談ですが、なぜwidth=0どこでも使用しているのですか?幅のない Entry/Label はほとんど役に立ちません。

于 2012-06-26T14:47:05.887 に答える