简体   繁体   中英

Why is frame not displaying on tkinter

I am trying to build 2048, I made the GUI with no problem, but it only displays the cells, and not the score frame I created. Looking over it, i cannot find anything wrong myself. I believe i properly created the frame and label, and I think it is written in the right spot, but i cannot for the life of me see anything wrong with it. Can someone tell me what I'm doing wrong before I move on?

import tkinter as tk

class Game(tk.Frame):
    def __init__(self):
        tk.Frame.__init__(self)
        self.grid()
        self.master.title("2048")

        self.main_grid = tk.Frame(
            self, bg="#a5c6e2", bd=3, width=600, height=600
        )
        self.main_grid.grid(pady=(100, 0))
        self.make_GUI()
        self.mainloop()
        

    def make_GUI(self):
        self.cells = []
        for i in range(4):
            row = []
            for j in range(4):
                cell_frame = tk.Frame(
                    self.main_grid,
                    bg="#5593c8",
                    width=150,
                    height=150
                )
                cell_frame.grid(row=i, column=j, padx=5, pady=5)
                cell_number = tk.Label(self.main_grid, bg="#3d84bf")
                cell_number.grid(row=i, column=j)
                cell_data = {"frame": cell_frame, "number":cell_number}
                row.append(cell_data)
            self.cells.append(row)


        score_frame = tk.Frame(self)
        score_frame.place(relx=0.5, rely=45, anchor="center")
        tk.Label(
            score_frame,
            text="score",
            font="helvetica"
        ).grid(row=0)
        self.score_label = tk.Label(score_frame, text="0", font="helvetica")
        self.score_label.grid(row=1)

Game()

The issue is in score_frame.place(relx=0.5, rely=45, anchor="center") . relx/rely may only be values between 0.0 and 1.0, as they are relative to your window size.

Setting rely to 45 will put your label about 45 window sizes outside the window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM