0

I am trying to use multiple buttons that all call the same function but have different parameters of 1, 2, 3, and so on. I want to have these parameters pass through the function in the class and only affect the object whose name matches that parameter. My issue is that I do not know how to use parameters with objects using self. I am new to programming and tkinter so any help, tips, or criticism is appreciated. If I messed up anything in my explanation please also let me know, thanks.

class Main(tk.Tk):
    def __init__(self):
        super().__init__()
        self.sub_app1= None
        self.sub_app2= None
        self.sub_app3= None
        b1 = tk.Button(self, text="Open Sub App One", command= lambda: self.sub_app_open(1))
        b1.pack(padx=20, pady=20)

    def sub_app_open(self, app):
        #self.sub_app = ("sub_app" + str(app))
        if self.sub_app(1) is None:
            print(type(self.sub_app1))
            self.sub_app = window1(self)

I will add more buttons calling that same function with a parameter that correlated to self.sub_app1, 2, and 3. I already tried doing sub_app + str(app) but my little knowledge of types let me down.

4
  • Don't use separate variables sub_app1, sub_app2, etc. Make it a list or dictionary, and pass the index or key as the app parameter.
    – Barmar
    Commented Jun 26, 2023 at 20:03
  • If you really want separate variables, you can use getattr(self, f"sub_app{app}")
    – Barmar
    Commented Jun 26, 2023 at 20:03
  • @Barmar If I was going to use a dictionary how would I pass the key/index as a parameter into the function? Commented Jun 27, 2023 at 11:15
  • The same way you're passing it now.
    – Barmar
    Commented Jun 27, 2023 at 15:07

1 Answer 1

0

The line if self.sub_app(1) is None: causes trouble here. The object self.sub_app does not exist in the first place, which is why you're getting error messages here. Trying to define it, as you did in the commented out line self.sub_app = ("sub_app" + str(app)) creates a string, but a string is not the same thing as a function. If you are continuing on this path, you need to "translate" the function name/string into a callable object. Assuming, the functions sub_app1, ... exist in the body of your class, you could do so with the vars function:

sub_app_name = "sub_app" + str(app)
func = vars(self)[sub_app_name]
func(self)

But this seems a bit convoluted. Is there a reason why you can't directly go command = self.sub_app1?

4
  • I plan to have many sub apps that all would open using that same function so I wanted to reduce having to make the function many times over for each sub app I create. I'm new to programming so I wasn't sure how to do this or if there is an easier way than trying to pass parameters into the function to distinguish each sub app. Commented Jun 27, 2023 at 11:17
  • But aren't you writing a new function for each of your sub-apps anyway? Anyway: if you want to run code depending on a parameter, the code needs to be in the form of a callable -- objects that you defined with lambda, def or the call method of a class. If you have several of those, you can store them in a dict: ``` def sub_proc_1(): pass def sub_proc_2(): pass procs = dict { 1 : sub_proc_1, 2 : sub_proc_2, } param = 1 selected_function = procs[param] selected_function() ``` Does that help you? Commented Jun 27, 2023 at 11:27
  • Once def sub_app_open runs it will call another class window{#}(self) in order to create another Toplevel. I will have many classes that do the same thing but have different code inside so I want to make where whatever button you click it passes its unique parameter so the function knows which class to use to create another window. Sorry if my explanations are confusing, I'm still not exactly sure on all the proper terminology to use. Commented Jun 27, 2023 at 11:33
  • 1
    getattr(self, sub_app_name) is a more preferred way to access the attribute.
    – Barmar
    Commented Jun 27, 2023 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.