简体   繁体   English

为什么 tkinter 上不显示框架

[英]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.我正在尝试构建 2048,我制作的 GUI 没有问题,但它只显示单元格,而不是我创建的分数框架。 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.我相信我正确地创建了框架和 label,我认为它写在正确的位置,但我一辈子都看不出它有什么问题。 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") .问题出在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. relx/rely 只能是 0.0 和 1.0 之间的值,因为它们与您的 window 大小有关。

Setting rely to 45 will put your label about 45 window sizes outside the window.将依赖设置为 45 将使您的 label 大约 45 window 大小在 window 之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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