[英]tkinter useing grid on a frame doesn't seem to work
我试图获得一个 tkinter gui,让用户登录我对 tkinter 真的很陌生。 我制作了一个框架,当我使用 grid 将另一个框架小部件放在框架内时,它是基于根而不是我认为正在发生的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()
你有初学者很常见的错误
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
编辑:
使用正确分配的小部件,我得到
现在灰色框在黄色框架内,但图像显示不同的问题 - grid()
/ pack()
自动计算位置和大小,外部框架自动更改大小以适合孩子的大小。
使用.grid_propagate(False)
你可以停止它
但它显示了其他问题 - 单元格不使用父级的完整大小,因此黄色框移到左上角,而不是居中:) 空单元格的width = 0
和heigh = 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 代码风格指南
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.