2012-04-23 4 views
1

Я пытаюсь создать игру Scrabble с Python. Я бы хотел отобразить точки, которые стоит, когда пользователь вводит слово. Я уже задавал этот вопрос, поскольку не знал, какой метод использовать. Поскольку я обнаружил, какой метод использовать, и мой вопрос о том, как использовать этот метод, я думаю, что это заслуживает нового вопроса. Моя проблема в том, что я создал функцию под названием bind_entry(event), которая должна устанавливать метку каждый раз, когда пользователь вводит букву. Но функция bind_entry(event) не знает установленную метку и запись, в которой находится слово.Python 2.7: Tkinter, Как использовать метод bind?

Вот мой код:

#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_word и val_points в функции bind_entry()?

+0

Я не совсем понимаю, что 'm' находится в' bind_entry'. Кроме того, какой ввод принимает '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

Отлично! Спасибо, метод лямбда был тем, что мне нужно. К сожалению, я не могу использовать метод класса, потому что у меня есть некоторые ограничения для моей работы: s. Еще раз спасибо за помощь;)! –

+0

@MalikFassi Я рад помочь. – mgilson

Смежные вопросы