I have to access the components contained by a tkinter.PanedWindow so I can modify the value of the amountused of the ttkbootsatrap.Meter with the method update().
我必须访问tkinter.PanedWindow包含的组件,这样我才能使用方法update()修改ttkbootsatrap.Meter的使用量的值。
Is there a funcition similar to the Java Swing JPanel.getComponents()
in tkinter or ttkbootstrap?
Tkinter或ttkbootstrap中是否有类似于Java Swing JPanel.getComponents()的函数?
Here is a snippet of my code:
以下是我的代码片段:
def buildChart(self):
chartPanel = tk.PanedWindow(self.parent_window, orient=tk.VERTICAL, borderwidth=0)
meter = ttk.Meter(
metersize = self.meter_radius,
padding = self.padding,
amountused = 25,
metertype = self.meter_type,
subtext = self.subtext,
interactive = self.isInteractive,
)
meter.pack(expand=True)
chartPanel.add(meter)
return chartPanel
def updateValue(panel, value):
for c in panel.getComponents(): # <- This is where i would implement the function instead of "panel.getComponents()" (which, ofcourse, does not work)
if isinstance(c, ttk.Meter):
meter.configure(amountused = value)
I come from a java background, hope this is not a dumb question.
Thank you in advance!
我来自Java背景,希望这不是一个愚蠢的问题。提前谢谢您!
更多回答
优秀答案推荐
The panes
method returns an ordered list of the widgets managed by the paned window. Unfortunately it returns a low level Tcl object rather than python objects.
Panes方法返回由窗格窗口管理的小部件的有序列表。遗憾的是,它返回的是低级TCL对象,而不是Python对象。
Fortunately, there's a fairly easy way to translate the low level object to the python object. The lower level object has a string
attribute that contains the internal name of the widget, or you can use the str
method on it. Python widgets have a method named nametowidget
which can take the internal name and convert it to the python object.
幸运的是,有一种相当简单的方法可以将低级对象转换为python对象。较低级别的对象有一个字符串属性,该属性包含小部件的内部名称,或者您可以对其使用str方法。Python小部件有一个名为nametowidget的方法,该方法可以获取内部名称并将其转换为Python对象。
So, using that information you can loop over the widgets like this (assuming panel
represents the PanedWindow widget):
因此,使用这些信息,您可以像这样在小部件上循环(假设panel代表PanedWindow小部件):
def updateValue(panel, value):
for obj in panel.panes():
c = panel.nametowidget(str(obj))
if isinstance(c, ttk.Meter):
...
更多回答
我是一名优秀的程序员,十分优秀!