如何让网格在 tkinter 中的容器内拉伸

问题描述

我正在尝试使用 python 创建一个程序,该程序稍后将控制设备中的泵,现在我只是想了解如何使用 tkinter 构建 GUI。 我已经把所有东西都显示出来了,除了我不能让按钮随着窗口伸展,我不能让文本停留在窗口的中心。我想我已经很接近让它工作了,但我已经坚持了一段时间。

代码是我在网上找到的,并做了一些调整,这就是为什么我被卡住了,因为我实际上不是程序员,不幸的是,我没有时间深入研究它。这就是我寻求帮助的原因。

如果有人可以提供一些建议,非常感谢!提前致谢。

这是我的代码

    import tkinter as tk
    from tkinter import ttk
    
    fonts =("Verdana",35)
    
    class application(tk.Tk):
    
        # __init__ function for class tkinterapp
        def __init__(self,*args,**kwargs):
    
            # __init__ function for class Tk
            tk.Tk.__init__(self,**kwargs)
    
            tk.Grid.rowconfigure(self,weight=1)
            tk.Grid.columnconfigure(self,weight=1)
    
            # creating a container
            container = tk.Frame(self)
            container.grid(row = 0,column = 0,sticky = "nsew")
    
            # initializing frames to an empty array
            self.frames = {}
    
            # iterating through a tuple consisting
            # of the different page layouts
            pages = (StartPage,Page1)
            for F in pages:
    
                frame = F(container,self)
    
                # initializing frame of that object from
                # startpage,page1,page2 respectively with
                # for loop
                frame.grid(row = 0,sticky ="nsew")
                self.frames[F] = frame
    
            self.show_frame(StartPage)
    
        # to display the current frame passed as
        # parameter
        def show_frame(self,cont):
            frame = self.frames[cont]
            frame.grid(sticky ="nsew")
            frame.tkraise()
    
    # first window frame startpage
    
    class StartPage(tk.Frame):
        def __init__(self,parent,controller):
            tk.Frame.__init__(self,parent)
            self.rowconfigure(1,weight=1)
            self.columnconfigure(0,weight=1)
            self.columnconfigure(1,weight=1)
    
            # label of frame Layout 2
            label = ttk.Label(self,text ="Startpage",font = fonts)
    
            # putting the grid in its place by using
            # grid
            label.grid(row = 0,padx = 10,pady = 10,columnspan = 2,sticky = "nsew")
    
            btn1 = ttk.Button(self,text ="Page 1",command = lambda : controller.show_frame(Page1))
    
            # putting the button in its place by
            # using grid
            btn1.grid(row = 1,sticky = "nsew")
    
            btn2 = ttk.Button(self,text ="Page test",command = lambda : controller.show_frame(Page1))
    
            # putting the button in its place by
            # using grid
            btn2.grid(row = 1,column = 1,sticky = "nsew")
    
    # second window frame page1
    class Page1(tk.Frame):
    
        def __init__(self,controller):
    
            tk.Frame.__init__(self,weight=1)
    
            label = ttk.Label(self,font = fonts)
            label.grid(row = 0,sticky = "nsew")
    
            # button to show frame 2 with text
            # layout2
            btn1 = ttk.Button(self,text ="Back",command = lambda : controller.show_frame(StartPage))
    
            # putting the button in its place by
            # using grid
            btn1.grid(row = 1,text ="Back 2",command = lambda : controller.show_frame(StartPage))
    
            # putting the button in its place by
            # using grid
            btn2.grid(row = 1,sticky = "nsew")
    
    # Driver Code
    app = application()
    app.mainloop()
    

解决方法

您需要致电:

container.rowconfigure(0,weight=1)
container.columnconfigure(0,weight=1)

否则,StartPagePage1 不会占用 container 的所有空间。

要使标签居中,您需要在 anchor="c" 中添加 ttk.Label(...)

...

label = ttk.Label(self,text ="Startpage",font=fonts,anchor="c")

...

label = ttk.Label(self,text ="Page 1",anchor="c")

...