2016-12-31 7 views
1

複数のコンボボックスに対して同じ選択イベントを使用しようとしています。出来ますか?イベントの送信者である関数に渡す方法が見つかりません。"ComboboxSelected"に1つの関数を使用して複数のコンボボックスを読み取る

def newselection(self, event, {????}): 
     self.selection = self.{????}.get() 
     print(self.selection) 

    self.combo1 = Combobox(self, width=7) 
    self.combo1.bind("<<ComboboxSelected>>", self.newselection({????})) 
    self.combo1['values'] = ('a', 'c', 'g', 't') 
    self.combo1.place(x=250, y=400) 
    self.combo1.state(['readonly']) 

    self.combo2 = Combobox(self, width=7) 
    self.combo2.bind("<<ComboboxSelected>>", self.newselection({????})) 
    self.combo2['values'] = ('X', 'Y', 'XX', 'XY') 
    self.combo2.place(x=450, y=400) 
    self.combo2.state(['readonly']) 

だから、それが選択されているコンボ問題ではありません私が正しくコンボボックスの値を割り当てることができますので、私は、同じ機能を使用すると、送信者を読み取ることができます。

答えて

2

bindは、ファンクション名を期待しています - ()と引数なしを意味します。しかし、それは、ウィジェットへのアクセスを与えるeventでこれfunctionwを実行 - event.widget

の作業例:

import tkinter as tk 
from tkinter import ttk 

# --- functions --- 

def newselection(event): 
    print('selected:', event.widget.get()) 

# --- main --- 

root = tk.Tk() 

cb1 = ttk.Combobox(root, values=('a', 'c', 'g', 't')) 
cb1.pack() 
cb1.bind("<<ComboboxSelected>>", newselection) 

cb2 = ttk.Combobox(root, values=('X', 'Y', 'XX', 'XY')) 
cb2.pack() 
cb2.bind("<<ComboboxSelected>>", newselection) 

root.mainloop() 

あなたはより多くの引数を必要とするならば、あなたはlambda

import tkinter as tk 
from tkinter import ttk 

# --- functions --- 

def newselection(event, other): 
    print('selected:', event.widget.get()) 
    print('other:', other) 

# --- main --- 

root = tk.Tk() 

cb1 = ttk.Combobox(root, values=('a', 'c', 'g', 't')) 
cb1.pack() 
cb1.bind("<<ComboboxSelected>>", lambda event:newselection(event, "Hello")) 

cb2 = ttk.Combobox(root, values=('X', 'Y', 'XX', 'XY')) 
cb2.pack() 
cb2.bind("<<ComboboxSelected>>", lambda event:newselection(event, "World")) 

root.mainloop() 
+0

恐ろしいを使用する必要があり、おかげでたくさん!完璧に動作します。どのように送信者の名前を取得するのですか?私は 'event.widget'を印刷しようとしましたが、名前自体ではなくオブジェクトを取得します。 –

+0

変数の名前は何ですか?なぜこの名前が必要ですか? 'event.widget'を使用すると、このウィジェットのすべてのプロパティにアクセスできます。 – furas

+0

BTW:ウィジェット、つまりで新しいプロパティを作成することもできます。 'cb1.my_name =" James Bond "'それから、あなたは 'print(event.widget.my_name)'関数でそれを使うことができます。 – furas

関連する問題