1

Python でスクラブル ゲームを作成しようとしています。ユーザーが単語を入力しているときに、その単語が値するポイントを表示したいと思います。どの方法を使用すればよいかわからなかったので、すでにこの質問をしました。どの方法を使用するかを発見したので、私の質問はこの方法の使用方法に関するものであるため、これは新しい質問に値すると思います。私の問題はbind_entry(event)、ユーザーが文字を入力するたびにラベルを設定するという関数を作成したことです。しかし、関数bind_entry(event)は、設定するラベルと、単語が存在するエントリを認識していません。

これが私のコードです:

#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_wordとは別のファイルの関数で作成されているので、関数でとval_pointsを呼び出すにはどうすればよいですか?val_entry_wordval_pointsbind_entry()

4

1 に答える 1

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"))        
于 2012-04-23T12:34:27.443 に答える