python中bind的用法_Python 2.7:Tkinter,如何使用bind方法?

I'm trying to create a Scrabble game with Python. I'd like to display the points that the word worth when the user is typing the word.

I already asked this question as I didn't know what method to use. As I discovered which method to use, and my question is about how to use this method, I think this deserve a new question.

My problem is that I created a function called bind_entry(event) that is supposed to set a label every time the user type a letter. But the function bind_entry(event)doesn't know the label to set and the entry where the word is.

Here is my code :

#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("",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)

I just noticed that the function m.counting_points() will count only the letter that is typed by the user. Here I should call val_entry_word.

So here is my question :

As val_entry_word and val_points are created in a function in another file How could I call val_entry_word and val_points in the function bind_entry() ?

解决方案

Generally, when you need different function calls to share information without passing it explicitly, the best practice is to use a class.

e.g.

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

Of course, you could also use globals...(though that is definitely discouraged)

Finally, an idiom that I often use to add additional information in a bind callback is to wrap the callback function in another function...

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("",lambda e: change_label("Horse"))

lbl.bind("",lambda e: change_label("Cow"))

要在Pythontkinter添加超链接,你可以使用Text组件的tag_bind()方法来实现。首先,你需要在Text组件插入文本,然后使用tag_add()方法给要添加超链接的文本添加一个标签。接下来,使用tag_bind()方法将这个标签与一个回调函数绑定,这个回调函数会在用户点击超链接时被调用。在回调函数,你可以执行相应的操作,比如打开一个网页。 下面是一个示例代码,演示了如何在tkinter添加超链接: ```python import tkinter as tk def open_link(event): # 在这个回调函数执行打开链接的操作 print("打开链接") root = tk.Tk() text = tk.Text(root) text.pack() # 在Text组件插入文本 text.insert(tk.END, "Pythontkinter:") text.insert(tk.END, "tkinter带你进入GUI世界", "link") # 给文本添加一个标签 # 给标签添加超链接的样式 text.tag_config("link", foreground="blue", underline=True) # 绑定回调函数到标签 text.tag_bind("link", "<Button-1>", open_link) root.mainloop() ``` 在这个例子,我们在Text组件插入了两段文本,其一段文本被标记为"link",并且绑定了一个回调函数open_link()。当用户点击带有"link"标签的文本时,回调函数open_link()会被调用。你可以在这个回调函数执行你想要的操作,比如打开一个网页。 注意:在示例,回调函数open_link()只是简单地打印了一条消息。你可以根据自己的需求来修改这个函数,例如使用webbrowser模块在浏览器打开链接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值