2017-01-23 3 views
0

私の質問の例として、画面上にボタンを生成しています。すべてのボタンは全く同じコマンドを持ちますが、ウィンドウ内の異なる座標に配置されます(tkinterを使用)。以下のような変数を定義するのと同じ方法で使用して:同じ寸法およびコマンドでIボタンを定義することができる方法同様の変数を1行に定義する

apple, banana, pear = "fruit" 

、それぞれが、とそれがインクリメントされる座標。 私はこれらのボタンに、それは次のようになり一つずつ...

Button1 = Button(root, text= "Button", height = 1, width = 1, command = command, x = 20, y = 50) 
Button2 = Button(root, text= "Button", height = 1, width = 1, command = command, x = 40, y = 50) 
Button3 = Button(root, text= "Button", height = 1, width = 1, command = command, x = 60, y = 50) 

を定義することでした。しかしループに似たものを使用してこれらのボタンを定義する方法がある場合は?おかげさまで

+0

'Button'クラスは、 'X'と' y'引数をサポートしていません。これらの座標をどのように使用すると思いますか?行/列の値ですか?ピクセル値? –

+0

私はちょうどその一般的な考え方を得るための簡単な例として使っていました –

答えて

0

あなたは、リストを反復処理することができます

buttons = [] 
for p in [(20,50),(40,50),(60,50)]: 
    buttons += [Button(root, text= "Button", height = 1, width = 1, command = command, x = p[0], y = p[1])] 
1

を開梱して、リストの内包表記を使用してください。 (あなたの最初の例では、方法によって、ValueErrorを発生させます。)

Button1, Button2, Button3 = [Button(root, text="Button", height=1, width=1, command=command, x=x, y=y) 
           for x,y in [(20, 50), (40,50), (60,50)]] 
関連する問題