1

我正在尝试使用 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, *args, **kwargs)
    
            tk.Grid.rowconfigure(self, 0, weight=1)
            tk.Grid.columnconfigure(self, 0, 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, column = 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, column = 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, column = 0,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, parent, controller):
    
            tk.Frame.__init__(self, parent)
            self.rowconfigure(1, weight=1)
            self.columnconfigure(0, weight=1)
            self.columnconfigure(1, weight=1)
    
            label = ttk.Label(self, text ="Page 1", font = fonts)
            label.grid(row = 0, column = 0, padx = 10, pady = 10,columnspan = 2,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, column = 0,sticky = "nsew")
    
            btn2 = ttk.Button(self, text ="Back 2",
            command = lambda : controller.show_frame(StartPage))
    
            # putting the button in its place by
            # using grid
            btn2.grid(row = 1, column = 1,sticky = "nsew")
    
    # Driver Code
    app = application()
    app.mainloop()
    
4

1 回答 1

1

您需要致电:

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

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

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

...

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

...

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

...
于 2021-03-02T05:15:01.140 回答