2012-04-23 28 views
1

私はPythonでスクラブルゲームを作成しようとしています。ユーザーが単語を入力しているときに、その単語が意味する点を表示したいと思います。 どのような方法を使用するのか分かりませんでしたので、私はすでにこの質問をしました。私はどのメソッドを使うべきかを発見したので、このメソッドを使う方法について質問しましたが、これは新しい質問に値すると思います。 私の問題は、bind_entry(event)という関数を作成したことで、ユーザーが文字を入力するたびにラベルを設定するはずです。しかし、関数bind_entry(event)は、設定するラベルとその単語が入っている項目を知らない。ここでPython 2.7:Tkinter、バインドメソッドの使い方は?

が私のコードです:

#this the function creating the label 
def create_variabletext_intlabel(root,col,row): 
    val=IntVar() 
    label=Label(root,textvariable=val) 
    label.grid(column=col,row=row) 
    return val, label 

#this is the function creating the entry 
def create_entry_string(root,width,col,row,columnspan,rowspan): 
    val=StringVar() 
    entry=ttk.Entry(root,width=width,textvariable=val) 
    entry.grid(column=col,row=row,columnspan=columnspan,rowspan=rowspan) 
    entry.bind("<Any-KeyPress>",bind_entry) 
    #Here is my problem, when I call the function bind_entry. 
    return val, entry 

def bind_entry(event): 
    label.set(m.counting_point(char(event))) 
    # m.counting_point() is a function counting the word's points 
    # my problem is that the function doesn't know yet the label. 
    # I don't know how to call the label. 

# I call the function create_entry_string in another file initiating 
# all the widget for the GUI 
val_entry_word, entry_word =g.create_entry_string(root,15,1,1,1,1) 

# I call the function create_variabletext_intlabel in another file 
# initiating all the widget for the GUI 
val_points,label_points=g.create_variabletext_intlabel(root,1,2) 

私はちょうど機能m.counting_points()は、ユーザーが入力された文字のみをカウントすることに気づきました。ここで私はval_entry_wordと呼ぶべきです。 val_entry_wordval_pointsとして

は私が機能bind_entry()val_entry_wordval_pointsを呼び出すことができますどのように他のファイル内の関数で作成されますので、ここで

は私の質問ですか?

+0

私は 'bind_entry'に' m'が何であるのかよく分かりません。また、 'counting_point'にはどのような入力がありますか?論理推測は文字列ですが、あなたが何をしているのかは不明です。 'char'とは何ですか?私が言うことができる限り、それはPythonの組み込みではありません... – mgilson

答えて

4

一般に、明示的に渡さずに情報を共有するために異なる関数呼び出しが必要な場合は、クラスを使用することをお勧めします。

もちろん

class LabelUpdater(object): 
    def create_variabletext_intlabel(self,root,col,row): 
     val=IntVar() 
     self.label=label=Label(root,textvariable=val) 
     label.grid(column=col,row=row) 
     return val, label 

    #this is the function creating the entry 
    def create_entry_string(self,root,width,col,row,columnspan,rowspan): 
     val=StringVar() 
     entry=ttk.Entry(root,width=width,textvariable=val) 
     entry.grid(column=col,row=row,columnspan=columnspan,rowspan=rowspan) 
     entry.bind("<Any-KeyPress>",self.bind_entry) 
     #Here is my problem, when I call the function bind_entry. 
     return val, entry 

    def bind_entry(self,event): 
     self.label.set(m.counting_point(char(event))) 

#At this point, I don't understand your code anymore since I don't know what g 
#is or how it's create_entry_string method calls your create_entry_string function... 
#I'll assume that the module where g's class is defined imports this file... 
#If that's how it works, then the following may be ok, although probably not because 
#of circular imports... 

container=LabelUpdater() 
create_variabletext_intlabel=container.create_variabletext_intlabel 
create_entry_string=container.create_entry_string 

val_entry_word, entry_word =g.create_entry_string(root,15,1,1,1,1) #somehow calls create_variabletext_intlabel which is mapped to container.create_variable_intlabel??? 

# I call the function create_variabletext_intlabel in another file 
# initiating all the widget for the GUI 
val_points,label_points=g.create_variabletext_intlabel(root,1,2) 

(それは確かに落胆ですが)、あなたも...グローバルを使用することができ

最後に、私は多くの場合、バインドコールバックで追加情報を追加するために使用イディオムは、コールバックをラップすることです他の関数の関数...

def myfunc(root): 
    label=Label(root,text="cow") 
    label.pack() 
    return label 

#This is the callback we want... 
# Q: but how do we pass S? 
# A: we need to wrap this call in another -- a perfect use for lambda functions! 
def change_label(label,S): 
    label.config(text=S) 

root=Tk() 
lbl=myfunc(root) 
lbl.bind("<Enter>",lambda e: change_label("Horse")) 
lbl.bind("<Leave>",lambda e: change_label("Cow"))   
+0

素晴らしい!ありがとう、ラムダメソッドは私が必要なものでした。残念ながら、クラスメソッドを使用することはできません。私は自分の仕事にいくつかの制約があるからです。 もう一度あなたの助けをありがとう;)! –

+0

@MalikFassi私は助けてうれしいです。 – mgilson

関連する問題