PyBeard
PyBeard

Reputation: 13

Widget in a second frame in a main class does not appear

I am trying to add a second frame inside my main class and put there a few widgets. I created a frame by using a method and assigned one of the widget to that frame but the problem is it does not appear.

I provided below piece of code with window configuration and 2x Labels which are at the main frame (Both appear correctly) and one in the new frame which appearing problem.

If you have some idea, please help me :)

import tkinter as tk

class MainApplication(tk.Tk):

    def __init__(self):
        super().__init__()

        # Adding a background picture
        self.background_img = tk.PhotoImage(file="in office.png")
        back_ground_img_label = tk.Label(self, image=self.background_img)
        back_ground_img_label.pack(fill="both", expand=True)

        # Adjusting the window
        width_of_window = 1012
        height_of_window = 604
        screen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()
        x_coordinate = int((screen_width / 2) - (width_of_window / 2))
        y_coordinate = int((screen_height / 2) - (height_of_window / 2) - 30)
        self.geometry(
            f"{width_of_window}x{height_of_window}+{x_coordinate}+{y_coordinate}"
        )

        self.bet_frame()

        bet_value_label_bg = tk.Label(self)
        bet_value_label_bg.place(x=462, y=300)
        coin_button_1 = tk.Button(self.frame)
        coin_button_1.place(x=233, y=435)

    def bet_frame(self):
        self.frame = tk.Frame(width=1012, height=604)
        self.frame.pack()


if __name__ == "__main__":
    MainApplication().mainloop()

Upvotes: 0

Views: 63

Answers (1)

Bruno Vermeulen
Bruno Vermeulen

Reputation: 3465

The only thing you put in the self.frame is the coin_button_1, but as you place it at (233, 435) is is hidden below the main window self.

Personally I would not use place but rather either pack or even better grid to place the widgets on the screen (see Setting Frame width and height)

So if you change def bet_frame(self) as follows it will be visible

...
        bet_value_label_bg = tk.Label(self, text='value')
        bet_value_label_bg.place(x=462, y=300)

    def bet_frame(self):
        self.frame = tk.Frame(master=self, width=1012, height=604)
        self.frame.pack()
        coin_button_1 = tk.Button(self.frame, text='coin button')
        coin_button_1.pack()
...

Note the bet_value_label_bg shows up in the middle of the picture and you may have to expand the main window to make the self.frame visible, depending on the size of the picture.

Upvotes: 1

Related Questions