如何在事件中获取小部件名称 [英] How to get widget name in event

查看:19
本文介绍了如何在事件中获取小部件名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from tkinter import *

main = Tk()

def flipper(event):

    # I'd like to do this:
    #if widgetname == switcher:
        #do stuff
    #if widgetname == switcher1:
        #do stuff
    return

switcher = Label(main, bg='white', text="click here", font="-weight bold")
switcher.grid()
switcher.bind("<Button-1>", flipper)


switcher1 = Label(main, bg='white', text="click here", font="-weight bold")
switcher1.grid()
switcher1.bind("<Button-1>", flipper)

switcher2 = Label(main, bg='white', text="click here", font="-weight bold")
switcher2.grid()
switcher2.bind("<Button-1>", flipper)

switcher3 = Label(main, bg='white', text="click here", font="-weight bold")
switcher3.grid()
switcher3.bind("<Button-1>", flipper)

switcher4 = Label(main, bg='white', text="click here", font="-weight bold")
switcher4.grid()
switcher4.bind("<Button-1>", flipper)

switcher5 = Label(main, bg='white', text="click here", font="-weight bold")
switcher5.grid()
switcher5.bind("<Button-1>", flipper)


main.mainloop()

在我的事件函数中,我想根据点击的标签做不同的事情.我难倒的是我只能得到被点击的小部件的标识符号,而不是名称.如果我可以获得所有小部件的标识符,那么我可以这样做:

In my event function i'd like to do different things based on the label that is clicked. What im stumped on is that I can only get the identifier number of the widget that is clicked, not the name. If I could get the identifier of all my widgets then I could do:

def flipper(event):
    if event.widget == switcher.identifier():
           do stuff

但我也找不到如何获取指定小部件的 id...

but I cant find how to get the id of a specified widget either...

如何通过标识符获取小部件的名称(event.widget())?

How can I get the name of a widget by its identifier(event.widget())?

或者如何获取指定小部件名称的标识符?

OR how can I get the identifier of a specified widget name?

如果两者都不可能,那么我将不得不为每个标签创建一个不同的函数并绑定,这是很多工作,希望不是必需的.

If neither are possible then i'd have to make a different function and bind for each label which is a lot of work that hopefully is not necessary.

from tkinter import *

main = Tk()

def flipper(event, switch):
    if switch.widget == 's1':
        print("got it")

switcher = Label(main, bg='white', text="click here", font="-weight bold")
switcher.grid()
switcher.bind("<Button-1>", flipper)
switcher.widget = 's1'


main.mainloop()

推荐答案

你不能得到分配给widget的变量名,那比较没用.一个小部件可以分配给多个变量,或者根本不分配.

You can't get the variable name that the widget is assigned to, that would be relatively useless. A widget could be assigned to more than one variable, or none at all.

您可以访问实际的小部件,并且可以使用它来获取标签上的文本.您的示例显示所有标签都相同,因此这可能对您没有用:

You have access to the actual widget, and you can use that to get the text that is on the label. Your example shows that all labels are the same, so this might not be useful to you:

def flipper(event):
    print("label text:", event.widget.cget("text"))

使用自定义小部件名称

您还可以为小部件命名.您无法准确取回名称,但可以非常接近.例如,如果您创建这样的标签:

Using a custom widget name

You can also give a widget a name. You can't get back precisely the name, but you can come very close. For example, if you create a label like this:

switcher = Label(main, name="switcher", bg='white', text="click here", font="-weight bold")

您可以通过拆分."来获取小部件的字符串表示形式.并取最后一个值:

You can get the string representation of the widget by splitting on "." and taking the last value:

def flipper(event):
    print("widget name:", str(event.widget).split(".")[-1])

通过绑定传递名称

最后,您可以设置绑定,以便将名称发送到函数:

Passing a name via the binding

Finally, you can set up your bindings such that the name is sent to the function:

switcher.bind("<Button-1>", lambda event: flipper(event, "switcher"))
switcher1.bind("<Button-1>", lambda event: flipper(event, "switcher1"))

这篇关于如何在事件中获取小部件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆