2012-04-23 37 views
1

我想用Python创建一个Scrabble游戏。我想在用户输入单词时显示该单词的价值。 我已经问过这个问题,因为我不知道使用什么方法。当我发现使用哪种方法时,我的问题是如何使用这种方法,我认为这有一个新问题。 我的问题是我创建了一个名为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'中的'm'是什么。此外,'counting_point'采取了哪些输入 - 逻辑猜测是一个字符串,但不清楚你正在做什么......例如,什么是'char'。据我所知,这不是一个蟒蛇内置... – mgilson 2012-04-23 12:21:12

回答

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

太棒了!谢谢你,lambda方法是我需要的。不幸的是,我不能使用类方法,因为我对我的工作有一些限制:s。 再次感谢您的帮助;)! – 2012-04-25 16:54:28

+0

@MalikFassi我很高兴得到了帮助。 – mgilson 2012-04-25 17:03:09