在框架上使用网格似乎不起作用

2024-06-16 15:29:01 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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()

Tags: intest框架tkintercolumnrootthisframe
1条回答
网友
1楼 · 发布于 2024-06-16 15:29:01

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

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

它将None赋值给inner_frame,因为grid()/pack()/place()给出了None

所以后面的Frame(inner_frame, ..)意味着Frame(None, ..),它增加了root

你必须分两步来做

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

现在您已经将Frame分配给了inner_frame


编辑:

通过正确分配的小部件,我可以

enter image description here

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

使用.grid_propagate(False)可以停止它

enter image description here

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

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

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

enter image description here


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 Style Guide for Python Code

相关问题 更多 >