2017-11-06 3 views
-1

私は現在、このコードをユーザから受け取り、入力されたボタンの量を生成します。そして、各ボタンが送信可能なフィールドを持つことを望みます。変数として戻って、しかし、以下のコードは何らかの理由でテキストフィールドの値を受け取っていませんが、同じ方法でそれを試してみましたが...私は何かを見逃してしまったのですか?方法?Python Tkinterは1つをマッチさせるのではなくランダムな入力を返します

(私はPython tkinter creating buttons in for loop passing command argumentsを使用しようとしましたが、何らかの理由で入力から文字列を返すときに、それらを混ぜて返します。例えば、4の代わりにテキスト入力フィールド3の値を返します。 )

from tkinter import * 
from random import randint 


inputValue = randint(3, 9) 


root = Tk() 


def retrieve_input2(inputed): 
    global buttons # get the dict 
    global Texts # get the dict 
    Texts.reverse() 
    print(inputed) # print what number button was pressed 
    inputed = int(inputed) - 1 
    inputvalue2 = Texts[inputed].get("1.0", "end-1c") # get the inputted value corresponding to button pressed 
    print(inputvalue2) # print the value that was entered 


buttons = [] # create empty dicts 
Texts = [] # create empty dicts 


while inputValue > 0: # for every number in inputted value 
    parse = str(inputValue) # create a string version of the input value that can be parsed to the retrieve function 

    button = Button(root, text="Lap :" + str(inputValue), command=lambda parse = parse: retrieve_input2(parse)) # create button variable 
    Texti = Text(root, height=1, width=5) # Create Text Variable 

    button.grid(row=inputValue, column=1) # put the button on the gui 
    Texti.grid(row=inputValue, column=0,) # Put the field on the gui 

    Texts.append(Texti) # append Value to dict 
    buttons.append(button) # append Value to dict 

    inputValue = int(inputValue) - 1 # take one 


root.mainloop() # Does work (required) 

答えて

1

まず、あなたはリストを使用して、それらに辞書を呼び出しています。あなたはfor()を通るたびに "button"と "texti"を重ね合わせるので、最後には最後のButtonとTextしかありません。このコードは、ボタン/テキスト番号とテキストインスタンスを辞書に格納し、関数にクリックされたボタン番号を渡します。この番号は、その番号に対応するテキストの辞書から内容を取得できます。

from tkinter import * 
from functools import partial 
from random import randint 

inputValue = randint(3, 9) 

root = Tk() 


def retrieve_input2(button_num): 
## Texts.reverse() 
    print(button_num) # print what number button was pressed 
## inputed = int(inputed) - 1 
    inputvalue2 = texts[button_num].get("1.0", "end-1c") # get the inputted value corresponding to button pressed 
    print(inputvalue2) # print the value that was entered 


##buttons = {} # create empty dicts 
texts = {} # create empty dicts 


for ctr in range(inputValue): 
    #parse = str(inputValue) # create a string version of the input value that can be parsed to the retrieve function 

    button = Button(root, text="Lap :" + str(ctr+1), bg="lightyellow", 
        command=partial(retrieve_input2, ctr)) # ctr=Button number 
    texti = Text(root, height=1, width=5) # Create Text Variable 

    button.grid(row=ctr, column=1) # put the button on the gui 

    texti.grid(row=ctr, column=0,) # Put the field on the gui 

    texts[ctr]=texti # ctr = button num --> this Text instance 
## buttons.append(button) # append Value to dict 


root.mainloop() 
+0

おかげで、そのので、代わりにキー例えば「1」、値prinitingと呼ばれる辞書の「NewDictを[button_num = inputvalue2]」追加seccond辞書を作成する必要がありました:」テキスト1' 、テキストを入力された '1': 'Kieron' – ItzKmaf

関連する問題