Switch to a new frame with specific information depending on the frame clicked

Viewed 22

I have a frame that acts as a page. This frame has frames inside that act as information containers by means of using labels. Each label's text is one value of a database entry.

I am trying to switch to a new frame with different information depending on the frame I click. I want to start by creating a label in the new frame with the same database entry value as the first label of the frame clicked.

My code is the following:

class Root(Tk):
    def __init__(self):
        Tk.__init__(self)
        ...
        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = Frame(self, background='#004445')
        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 (LoginWindow, RegisterWindow, Bedrijventerreinen, Dashboard, Bedrijf):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("LoginWindow")

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

class Bedrijventerreinen(Frame):
    ... 
    def show(self):
        self.controller
        x = 0
        y = 0
        # this code creates a frame one next to the other for each entry in my database table
        for index, bedrijventerrein in enumerate(result):
            if index <= 4:
                self.bedrijventerrein_frame = Frame(self.subbeige_frame, width=300, height=150, bg='white')
                self.bedrijventerrein_frame.grid(row=x, column=y, padx=16, pady=10)
                self.bedrijventerrein_frame.grid_propagate(0)
                # this event below allows a new frame to open when one of the frames above is clicked
                self.bedrijventerrein_frame.bind('<Button-1>', lambda evnt: self.controller.show_frame("Bedrijf"))
                self.bedrijventerrein_frame.bind('<Button-1>', lambda evnt, n=bedrijventerrein[1:2]:
                                                 b.func(n), add='+'), 


class Bedrijf(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller
        ...

    def bedrijf(self, event):
        self.group_number = StringVar()
        ...

        # photo side bar
        self.frame_photo = PhotoImage(file='Frame2.png')
        self.frame_label = Label(self.white_frame, image=self.frame_photo, bg='#F5F5F5')
        self.frame_label.pack(fill=BOTH, expand=TRUE)
        self.bedrijf_label= Label(self.white_frame, textvariable=self.group_number, font=('Calibri', 32, 'bold'),
                                  bg='green')
        self.bedrijf_label.grid()

    def func(self, event, n):
        self.group_number.set(n)

b = Bedrijf()

As you can see, I have used the code in Switch between two frames in tkinter? to switch between frames, and I have tried to adapt the code supplied in Tkinter Update list of buttons depending on a click of another list of buttons to achieve the functionality I am looking for.

When I run this code and I click on one of the bedrijventerrein_frame(s), I do get inside the frame "Bedrijf" where it appears the label created in the method "bedrijf" inside the class "Bedrijf", but with no text. Moreover, I receive the following error: TypeError: Bedrijf.func() missing 2 required positional arguments: 'event' and 'n'.

Could someone please tell/explain to me what I am doing wrong?

Any extra advice (explanations, resources...) that would support me on what I am trying to build, would also be much appreciated.

Cheers!

0 Answers
Related