框架的放置是意外的

Placement of Frame is unexpected

这就是我想要达到的效果:


到目前为止我做了什么


问题:


我正在尝试使用 pack 创建这个简单的 Gui 来放置,但放置对我来说是个问题,只需在下面发布代码。

from ctypes import windll
from tkinter import Tk, ttk

import tkextrafont


class aboutUs(Tk):
    def __init__(self):
        super().__init__()

        self.background_color = "#514C4C"
        self.title("About Us")
        self.geometry("500x500")
        self.aboutUs = 'About us'
        self.configure(background=self.background_color)
        self.resizable(False, False)

        # UI Styles and variables
        self.background_color = "#514C4C"
        self.background_color2 = "#D9D9D9"

        self.style = ttk.Style()

        # resources
        self.frameFonts1 = tkextrafont.Font(file="resources/Roboto_Mono/static/RobotoMono-Regular.ttf")

        # calling the components
        self.uppFrame()
        self.middleFrame()

    def uppFrame(self):
        aboutFrame = ttk.Frame(self,
                               style="TFrame",
                               height=111,
                               width=500)
        self.style.configure("TFrame", background=self.background_color)

        aboutFrame.pack(side="top", fill="both")
        ttk.Label(aboutFrame,
                  text="About Us",
                  font=('Roboto Mono', 40),
                  background=self.background_color,
                  foreground="white").place(relx=0.5, rely=0.5, anchor="center")

    def middleFrame(self):
        sepFrame = ttk.Frame(self, style="TFrame")
        self.style.configure("TFrame", background=self.background_color2, height=60, width=500)
        sepFrame.pack(side="bottom", fill="both")


if __name__ == '__main__':
    windll.shcore.SetProcessDpiAwareness(1)  # This line is important as it allows not to make fonts blurry
    aboutUsWindow = aboutUs()
    aboutUsWindow.mainloop()

希望大家能帮帮我!

我发现了我的错误:

  • 上下框架的样式名称相同"TFrame"

  • 在框架中而不是在样式中指定高度和宽度

    from ctypes import windll
     from tkinter import Tk, ttk
    
     import tkextrafont
    
    
     class aboutUs(Tk):
         def __init__(self):
             super().__init__()
    
             self.background_color = "#514C4C"
             self.title("About Us")
             self.geometry("500x500")
             self.aboutUs = 'About us'
             self.configure(background=self.background_color)
             self.resizable(False, False)
    
             # UI Styles and variables
             self.background_color = "#514C4C"
             self.background_color2 = "#D9D9D9"
    
             self.style = ttk.Style()
    
             # resources
             self.frameFonts1 = tkextrafont.Font(file="resources/Roboto_Mono/static/RobotoMono-Regular.ttf")
    
             # calling the components
             self.uppFrame()
             self.middleFrame()
    
         def uppFrame(self):
             aboutFrame = ttk.Frame(self,
                                    style="TFrame",
                                    height=111,
                                    width=500)
             self.style.configure("uppFrame.TFrame", background=self.background_color)
    
             aboutFrame.pack(side="top", fill="both")
             ttk.Label(aboutFrame,
                       text="About Us",
                       font=('Roboto Mono', 40),
                       background=self.background_color,
                       foreground="white").place(relx=0.5, rely=0.5, anchor="center")
    
         def middleFrame(self):
             sepFrame = ttk.Frame(self, style="TFrame", height=60, width=500)
             self.style.configure("midFrame.TFrame", background=self.background_color2)
             sepFrame.pack(side="bottom", fill="both")
    
    
     if __name__ == '__main__':
         windll.shcore.SetProcessDpiAwareness(1)  # This line is important as it allows not to make fonts blurry
         aboutUsWindow = aboutUs()
         aboutUsWindow.mainloop()