gpt4 book ai didi

Is there a way to access the Components contained by a PanedWindow in python tkinter?(有没有办法访问由python tkinter中的PanedWindow包含的组件?)

转载 作者:bug小助手 更新时间:2023-10-28 09:32:06 26 4
gpt4 key购买 nike



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

因此,使用该信息可以循环遍历小部件,如下所示(假设面板表示PanedWindow小部件):


def updateValue(panel, value):
for obj in panel.panes():
c = panel.nametowidget(str(obj))
if isinstance(c, ttk.Meter):
...

更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com