Python 2.7 : Tkinter, bind方法如何使用?

标签 python tkinter bind

我正在尝试使用 Python 创建拼字游戏。我想在用户输入单词时显示单词的值(value)。 我已经问过这个问题,因为我不知道使用什么方法。当我发现要使用哪种方法时,我的问题是关于如何使用这种方法,我认为这值得一个新问题。 我的问题是我创建了一个名为 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_wordval_points 是在另一个文件的函数中创建的,我如何调用 val_entry_wordval_points在函数 bind_entry() 中 ?

最佳答案

通常,当您需要不同的函数调用来共享信息而不显式传递信息时,最佳做法是使用类。

例如

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)

当然,您也可以使用全局变量...(尽管绝对不鼓励这样做)

最后,我经常用来在绑定(bind)回调中添加附加信息的一个习惯用法是将回调函数包装在另一个函数中......

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"))        

关于Python 2.7 : Tkinter, bind方法如何使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10277318/

相关文章:

python - Keras 验证损失计算不正确

python - 你如何在 python 中的 plotly 框中设置框宽度?

python - 如何使倒计时显示在 tkinter 窗口而不是我的 cmd 中?

python - (实例化一组按钮,但只有一个有效

python - 尝试在我的 Tkinter/Python 应用程序中嵌入 native WINDOWS 命令​​行

c++ - 绑定(bind)与 Lambda?

python - django 中的多语言 url 如何工作?

Python/Kivy : Call function from one class to another class and show widget in Python

dns - dig 查询中的附加记录数量不匹配

Angular 8, Jasmine <spyOn> : could not find an object to spy upon for bind()