Search code examples
pythonswingtkinterparent-childttkbootstrap

Is there a way to access the Components contained by a PanedWindow in python tkinter?


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().

Is there a funcition similar to the Java Swing JPanel.getComponents() in tkinter or ttkbootstrap?

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!


Solution

  • 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.

    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.

    So, using that information you can loop over the widgets like this (assuming panel represents the PanedWindow widget):

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