首页 > 解决方案 > tkinter 在框架上使用网格似乎不起作用

问题描述

我试图获得一个 tkinter gui,让用户登录我对 tkinter 真的很陌生。我制作了一个框架,当我使用网格将另一个框架小部件放入框架内时,它基于根而不是我认为正在发生的 inner_frame。在代码中,我制作了一个灰色框来演示,但我不明白为什么它位于蓝色框下方而不是“登录”文本下方的黄色框内。谢谢您的帮助。

from tkinter import *
root = Tk()
root.title("sighn in test")

#colors
background = "#273E47"
accent = "#d8973c"
red = "#bb4430"
white = "#edf2f4"

#this creates and places the background frame
main_buttons_frame = Frame(root, height = 500, width = 400, bg = background).grid(row = 0, column = 0)
#this creates and places the inner frame
inner_frame = Frame(main_buttons_frame, height = 450, width = 300, bg = accent).grid(row = 0, column = 0)
#this creates and places the "sighn in text"
top_text = Label(inner_frame, text = "sign in", font = ("helvitica", 30, "bold"), bg = accent, fg =         
background).grid(row = 0, column = 0)
#this is a test to demonstrate
test_frame = Frame(inner_frame, bg = "grey", height = 100, width = 100).grid(row = 1, column = 0)


root.mainloop()

标签: pythonuser-interfacetkintergridframes

解决方案


你有一个非常常见的初学者错误

inner_frame = Frame(...).grid...()

它分配Noneinner_frame因为grid()//给pack().place()None

所以后来的Frame(inner_frame, ..)意思是Frame(None, ..)它增加了root

您必须分两步完成

inner_frame = Frame(...)
inner_frame.grid(...)

现在你已经Frame分配到inner_frame


编辑:

使用正确分配的小部件,我得到

在此处输入图像描述

现在灰色框在黄色框架内,但图像显示不同的问题 - grid()/pack()自动计算位置和大小,外部框架自动更改大小以适合孩子的大小。

使用.grid_propagate(False)你可以阻止它

在此处输入图像描述

但它显示了其他问题 - 单元格不使用父级的完整大小,因此黄色框架移动到左上角,而不是居中:) 空单元格有width = 0heigh = 0因此将所有单元格移动到下一行和下一列不会改变它 - 它将是还在左上角:)

您需要分配weight给决定如何使用可用空间的列和/或行。所有列/行都有weight=0,当您设置时,weight=1此行和/或列将使用所有可用空间 - (这需要更好的解释) - 然后单元格内的元素将居中。

 main_buttons_frame.grid_columnconfigure(0, weight=1)
 main_buttons_frame.grid_rowconfigure(0, weight=1)

在此处输入图像描述


import tkinter as tk # PEP8: `import *` is not preferred

# --- colors ---

background = "#273E47"
accent = "#d8973c"
red = "#bb4430"
white = "#edf2f4"

# --- main ---

root = tk.Tk()
root.title("sighn in test")

main_buttons_frame = tk.Frame(root, height=500, width=400, bg=background) # PEP8: without spaces around `=` inside `( )`
main_buttons_frame.grid(row=0, column=0)
#main_buttons_frame = None
main_buttons_frame.grid_propagate(False)
main_buttons_frame.grid_columnconfigure(0, weight=1)
main_buttons_frame.grid_rowconfigure(0, weight=1)

inner_frame = tk.Frame(main_buttons_frame, height=450, width=300, bg=accent)
inner_frame.grid(row=0, column=0)
#inner_frame = None
inner_frame.grid_propagate(False)
inner_frame.grid_columnconfigure(0, weight=1)
inner_frame.grid_rowconfigure(0, weight=1)

top_text = tk.Label(inner_frame, text="sign in", font=("helvitica", 30, "bold"), bg=accent, fg=background)
top_text.grid(row=0, column=0,)
#top_text = None
#top_text.grid_propagate(False)
#top_text.grid_columnconfigure(0, weight=1)
#top_text.grid_rowconfigure(0, weight=1)

test_frame = tk.Frame(inner_frame, bg="grey", height=100, width=100)
test_frame.grid(row=1, column=0)
#test_frame = None
#test_frame.grid_propagate(False)
#test_frame.grid_columnconfigure(1, weight=1)
#test_frame.grid_rowconfigure(0, weight=1)

root.mainloop()

顺便说一句: PEP 8——Python 代码风格指南


推荐阅读