如何在类中使用 tkinter 的 nametwidget 函数获取小部件名称 [英] How to get a widget name with tkinter's nametowidget function within a Class

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

问题描述

我正在一个类中编写一个 tkinter 迷你项目,目前我正在尝试获取按钮小部件的名称,但是每次我尝试运行该程序时都会出现 Traceback 错误.我很好奇如何修复 nametwidget 行以获得小部件的正确名称.

I'm writing a tkinter mini project within a class, and am currently trying to get the name of a button widget but I get a Traceback error every time the program I try to run the program. I was curious as to how to go about fixing the nametowidget line to get the proper name of the widget.

from tkinter import *
import tkinter.messagebox
root = Tk()
class WindowPane(Frame):
    def __init__(self,master):
        Frame.__init__(self,master)
        self.master = master
        self.WindowWidget()

    def WindowWidget(self):
        self.stringer = StringVar()
        self.button1 = Button(text = "Click me")
        self.button1.place(x=200,y=50)
        print(self.master.nametowidget(".button1"))
root.geometry("200x200")
playback = WindowPane(root)
root.mainloop()

推荐答案

nametowidget 中的名称"不是用于保持对小部件的引用的变量的名称(即:在这个例子不是button1").这是有道理的,因为您可以让多个变量都指向同一个对象——python 怎么知道您想要哪个名称?

The "name" in nametowidget isn't the name of the variable used to keep a reference to the widget (ie: in this example it's not "button1"). This makes sense because you can have multiple variables all pointing to the same object - how would python know which name you want?

名称是指嵌入式 tcl/tk 解释器使用的内部小部件名称.通常,这是根据父级的名称、小部件类和可选数字计算的.例如,默认情况下您创建的第一帧的名称为 .!frame,下一帧的名称为 .!frame2,依此类推.第一帧内的第一个按钮将命名为 .!frame1.!button1 等.

The name refers to the internal widget name used by the embedded tcl/tk interpreter. Normally this is computed based on the name of the parent, the widget class and an optional number. For example, the first frame you create by default will have the name .!frame, the next frame will have the name .!frame2, and so on. The first button inside the first frame will be named .!frame1.!button1, etc.

您可以通过打印其字符串表示来查看任何小部件的名称.例如,在您的代码中,您可以执行 print(str(self.button1) 这将显示名称实际上是 .!button

You can see the name of any widget by printing out its string representation. For example, in your code, you could do print(str(self.button1) which will show you that the name is actually .!button

您不能使用 tkinter 中内置的任何内容将诸如 "button1" 之类的字符串转换为实际的小部件.但是self.button1这样的变量是当前对象的一个​​属性,所以可以使用python内置的getattr函数来获取一个属性的值给定的名字.

You can't use anything built into tkinter to convert a string like "button1" to the actual widget. However, a variable such as self.button1 is an attribute of the current object, so you can use python's built-in getattr function to get the value of an attribute with a given name.

在您的情况下,您可以使用 getattr(self, "button1") 来获取对实际小部件对象的引用.

In your case you can use getattr(self, "button1") to get a reference to the actual widget object.

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

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