简体   繁体   English

为什么我的 tkinter 框架没有填满整个窗口

[英]Why doesn't my tkinter frame fill the entire window

I am trying to create a quiz game but cannot get the labels to be in the right place, I want 'Maths Category' to be in the top left, but when I select the row and column 0 it puts it in the middle?我正在尝试创建一个问答游戏,但无法将标签放在正确的位置,我希望“数学类别”位于左上角,但是当我选择第 0 行和第 0 列时,它会将其放在中间? help....................................................................................................................................帮助................................................. ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………………………

import tkinter
import tkinter as tk
from tkinter.ttk import *
from tkinter import *




score = 0
class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame(MainMenu)

    def switch_frame(self, frame_class):
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()


class MainMenu(tk.Frame):
    def __init__(self, master):
        #Maths Picture/Button
        global photo
        tk.Frame.__init__(self, master)
        #configure = tk.Frame(self)
        #configure.grid_rowconfigure(0, weight=1)
        #configure.grid_columnconfigure(0, weight=1)
        photo = PhotoImage(file = "MathsPicture.png")
        photoimage = photo.subsample(3,3)
        lbl = tk.Label(self, text="MainMenu", font=('Verdana', 40, "bold"))
        lbl.grid(row=0, column=1)
        button = tk.Button(self, image = photo, command=lambda:[master.switch_frame(PageOne), ScoreUpdate()])
        button.grid(row=1, column=1)
        #tk.Button(self, text="Go to page two",command=lambda:[master.switch_frame(PageTwo), ScoreUpdate()]).pack()
        
def ScoreUpdate(event=None):
    global score
    score += 500

class PageOne(tk.Frame):
    global score
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Frame.configure(self,bg='red')
        #tk.Frame.configure
        tk.Frame.grid_propagate(self)
        lbl = tk.Label(self, text="Maths Category", font=("Verdana", 20))
        lbl.grid(row=0, column = 0)
        lbl = tk.Label(self, text=score , font=("Verdana", 40, "bold"))
        lbl.grid(row=1, column=0)
        lbl = tk.Label(self, text="Question 1", font=('Helvetica', 18, "bold"))
        lbl.grid(row=1, column=3)
        btn = tk.Button(self, text="Go back to MainMenu", font=('Helvetica', 20), command=lambda: master.switch_frame(MainMenu))
        btn.grid(row=0, column=4)
        btn = tk.Button(self, text='Update score', command=ScoreUpdate)
        btn.grid(row=3, column=4)
        btn = tk.Button(self, text="Go to page two",command=lambda:[master.switch_frame(PageTwo), ScoreUpdate()])
        btn.grid(row=4,column=3)
        print("Physics Question 1")

class PageTwo(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Frame.configure(self,bg='red')
        tk.Label(self, text="Question 2", font=('Helvetica', 18, "bold")).pack(side="top", fill="x", pady=5)
        tk.Button(self, text="Go back to start page",command=lambda: master.switch_frame(PageThree)).pack()
        tk.Button(self, text='Update score', command=ScoreUpdate)
        print("Physics Question 2")

class PageThree(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Frame.configure(self,bg='red')
        tk.Label(self, text="Question 3", font=('Helvetica', 18, "bold")).pack(side="top", fill="x", pady=5)
        tk.Button(self, text="Go back to start page",command=lambda: master.switch_frame(MainMenu)).pack()
        tk.Button(self, text='Update score', command=ScoreUpdate)
        print("Physics Question 3")



if __name__ == "__main__":
    window = SampleApp()
    window.geometry("1200x900")

    window.mainloop()

在此处输入图片说明

Grid should put the lbl.grid(row=0, column=0) in the top left corner. lbl.grid(row=0, column=0)该将lbl.grid(row=0, column=0)放在左上角。 It looks to me as if the PageOne object is centered on the window.在我看来, PageOne对象好像位于窗口的中心。

Try giving PageOne a background color to see how big it is and where its positioned.尝试给PageOne一个背景颜色,看看它有多大以及它的位置。 My guess is that the containing widget is centered in the window or that the containing frame (or window) is configured to expand (with columnconfigure ).我的猜测是包含小部件在窗口中居中,或者包含框架(或窗口)配置为展开(使用columnconfigure )。

Update更新

PageOne is packed into the root window ( SampleApp ) and by default pack will position it in the top middle. PageOne被打包到根窗口 ( SampleApp ) 中,默认情况下, pack会将其放置在顶部中间。 You can change this by specifying where you want it.您可以通过指定所需位置来更改此设置。

def switch_frame(self, frame_class):
    new_frame = frame_class(self)
    if self._frame is not None:
        self._frame.destroy()
    self._frame = new_frame
    self._frame.pack(expand=True, anchor='nw')  # New pack details

By setting the space assigned to the widget to expand it will try to get as much space as possible, and then I position the widget in the top right corner with anchor='nw' (as in north-west).通过将分配给小部件的空间设置为expand ,它将尝试获得尽可能多的空间,然后我将小部件放置在右上角的anchor='nw' (如在西北)。

But this places all frames in the upper left corner.但这会将所有帧放在左上角。 If you want this for only the PageOne widget you can adjust pack details there.如果您只想在PageOne小部件中使用此功能,您可以在那里调整包装详细信息。

class PageOne(tk.Frame):
    global score
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Frame.configure(self,bg='red')
        self.pack(expand=True, anchor='nw') # New pack details
        # etc, etc.

By default, a widget is centered in the space allocated to it.默认情况下,小部件在分配给它的空间中居中。 If you want a widget to stick to the top of the space rather than be in the middle, use the sticky attribute.如果您希望小部件粘在空间的顶部而不是中间,请使用sticky属性。

For example, the following will force the label to be at the upper-left (northwest) corner of the cell allocated to it.例如,以下将强制标签位于分配给它的单元格的左上(西北)角。

lbl.grid(row=0, column = 0, sticky="nw")

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

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