0

how can I get the value and placeholder in "optMenFunc" function from each iteration as value changes as I am working on below mentioned code, which has four classes, SampleApp, StartPage, PageOne, PageTwo. Page one has optionmenu with for loop. I am following the answer of this: Switch between two frames in tkinter


import tkinter as tk


class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is the start page")
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Go to Page One",
                            command=lambda: controller.show_frame("PageOne"))
        button2 = tk.Button(self, text="Go to Page Two",
                            command=lambda: controller.show_frame("PageTwo"))
        button1.pack()
        button2.pack()


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 1")
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()

        placeHolders = ['one', 'two']
        for placeHolder in placeHolders:

            self.options = tk.StringVar()
            self.options.set('select')
            self.menu = tk.OptionMenu(self, self.options, "miss", "mr", "mrs", command = self.optMenFunc)
            self.menu.pack()  

    def optMenFunc(self, value):
        print("i need to print here value with placeholder")


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()





jojo
  • 1

1 Answers1

0

You can use a lambda similar to how you did in the other commands.

self.menu = tk.OptionMenu(self, self.options, "miss", "mr", "mrs", command=lambda v, ph=placeHolder: self.optMenFunc(v, ph))

and

def optMenFunc(self, value, placeholder):
    print(f"value:{value} and placeholder:{placeholder}")

Note:

A: command=lambda v: self.optMenFunc(v, placeHolder)

B: command=lambda v, ph=placeHolder: self.optMenFunc(v, ph)

You have to set the argument in the lambda, as (B) is. If you do it the (A) way, placeHolder will equal whatever it's final iteration value is, regardless of which OptionMenu you select from. By making it an argument of the lambda you are saving it's current iteration value to ph.

OneMadGypsy
  • 4,640
  • 3
  • 10
  • 26