主类第二个框架中的小部件未出现

时间:2019-11-09 12:04:01

标签: python-3.x tkinter

我正在尝试在主类中添加第二个框架,并放置一些小部件。我使用一种方法创建了一个框架,并为该框架分配了一个小部件,但问题是它没有出现。

我在下面的代码中提供了窗口配置和位于主框架(两个都正确显示)上的两个标签,以及在新框架中出现问题的两个标签。

如果您有任何想法,请帮助我:)

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()

1 个答案:

答案 0 :(得分:1)

您放入self.frame中唯一的东西是coin_button_1,但是当您将其放置在(233,435)时,它隐藏在主窗口self下方。

我个人不会使用place,而是使用打包甚至更好的网格将小部件放置在屏幕上(请参见Setting Frame width and height

因此,如果您按以下方式更改jenkins,则将可见

def bet_frame(self)

请注意,bet_value_label_bg显示在图片的中间,您可能需要扩展主窗口以使self.frame可见,具体取决于图片的大小。