如何让网格在 tkinter 中的容器内伸展
How to get grid to stretch inside containers in tkinter
我正在尝试使用 python 创建一个程序,稍后将控制设备中的泵,我现在只是想了解如何使用 tkinter 构建 GUI。
我已经得到了所有要显示的东西,除了我不能用 window 使按钮伸展并且我不能让文本停留在 window 的中心。我想我已经非常接近让它工作了,但是已经坚持了一段时间。
代码是我在网上找到的,并做了一些调整,这就是为什么我被卡住了,因为我实际上不是程序员,不幸的是,我没有时间深入研究它。这就是我寻求帮助的原因。
如果有人可以提供一些建议,我们将不胜感激!提前致谢。
这是我的代码:
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()
您需要致电:
container.rowconfigure(0, weight=1)
container.columnconfigure(0, weight=1)
否则StartPage
和Page1
不会把container
的space全部拿走。
要使标签居中,您需要在 ttk.Label(...)
中添加 anchor="c"
:
...
label = ttk.Label(self, text ="Startpage", font=fonts, anchor="c")
...
label = ttk.Label(self, text ="Page 1", font=fonts, anchor="c")
...
我正在尝试使用 python 创建一个程序,稍后将控制设备中的泵,我现在只是想了解如何使用 tkinter 构建 GUI。 我已经得到了所有要显示的东西,除了我不能用 window 使按钮伸展并且我不能让文本停留在 window 的中心。我想我已经非常接近让它工作了,但是已经坚持了一段时间。
代码是我在网上找到的,并做了一些调整,这就是为什么我被卡住了,因为我实际上不是程序员,不幸的是,我没有时间深入研究它。这就是我寻求帮助的原因。
如果有人可以提供一些建议,我们将不胜感激!提前致谢。
这是我的代码:
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()
您需要致电:
container.rowconfigure(0, weight=1)
container.columnconfigure(0, weight=1)
否则StartPage
和Page1
不会把container
的space全部拿走。
要使标签居中,您需要在 ttk.Label(...)
中添加 anchor="c"
:
...
label = ttk.Label(self, text ="Startpage", font=fonts, anchor="c")
...
label = ttk.Label(self, text ="Page 1", font=fonts, anchor="c")
...