Search code examples
pythontkinternamed-parameters

Python: How to set function parameters by name with string


I'm making a framework for building GUI apps with tkinter and in my code I have a Scene class that has a add(widget,params) function which creates a tkinter widget and adds it to the Scenes widget dictionary. But I can only add a widget that I have built an add() function for because different widgets have different named parameters.

how would I make a function that takes a dictionary in the form {paramName,value} which would then pass it to the widget function

for example

scene.add(button, {'command':Click, "text":"click me"} )

my current code is

import tkinter as tk

class scene():
    def __init__(self, title, width, height):
        self.widgets = {}
        self.title = title
        self.width = width
        self.height = height

    def add(self, name, type, widget, root, params):
        if name in self.widgets.keys():
            return
        else:
            if type == "button":
                width = params[0]
                height = params[1]
                text = params[2]
                self.widgets[name] = [widget(root,width=width, height=height,text=text),params[2:]]

    def get(self,name):
        if name in self.widgets.keys():
            return self.widgets[name][0]

Solution

  • You can make use of setattr. See example below, where I have defined an if clause for the "generic" string:

    from tkinter import Tk, Button
    
    class scene():
        def __init__(self, title, width, height):
            self.widgets = {}
            self.title = title
            self.width = width
            self.height = height
    
        def add(self, name, type, widget, root, params, attributes={}):
            if name in self.widgets.keys():
                return
            else:
                if type == "button":
                    width = params[0]
                    height = params[1]
                    text = params[2]
                    self.widgets[name] = [widget(root,width=width, height=height,text=text),params[2:]]
                elif type == "generic":
                    width = params[0]
                    height = params[1]
                    text = params[2]
                    widget_obj = widget(root,width=width, height=height,text=text)
                    for key, val in attributes.items():
                        setattr(widget_obj, key, val)
                    self.widgets[name]=[widget_obj,params[2:]]
    
        def get(self,name):
            if name in self.widgets.keys():
                return self.widgets[name][0]
    
    # Create intance of tkinter
    root = Tk(className = 'Python Examples - Window 0')
    root.geometry("600x700")
    root.resizable(0,0)
    
    # Call class
    sc=scene("Some title", 100, 100)
    sc.add("button_widget", "button", Button, root, [10, 10, "some text"])
    sc.add("button_widget2", "generic", Button, root, [10, 10, "some text"], {"command": "click", "text": "click me"})