[英]tkinter useing grid on a frame doesn't seem to work
Im trying to get a tkinter gui that lets the user sign in im really new to tkinter.我试图获得一个 tkinter gui,让用户登录我对 tkinter 真的很陌生。 I made a frame and when i use grid to put another frame widget inside of the frame it does it based off of the root and not the inner_frame thats what I think is happening.
我制作了一个框架,当我使用 grid 将另一个框架小部件放在框架内时,它是基于根而不是我认为正在发生的inner_frame。 In the code I made a grey box to demonstrate and I dont understand why it is below the blue frame and not inside the yellow frame under the "sign in" text.
在代码中我做了一个灰色框来演示,我不明白为什么它在蓝框下方而不是在“登录”文本下的黄框内。 Thanks for the help.
谢谢您的帮助。
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()
You have very common mistake of beginners你有初学者很常见的错误
inner_frame = Frame(...).grid...()
It assigns None
to inner_frame
because grid()
/ pack()
/ place()
gives None
.它将
None
分配给inner_frame
因为grid()
/ pack()
/ place()
给出None
。
So later Frame(inner_frame, ..)
means Frame(None, ..)
and it adds to root
所以后来
Frame(inner_frame, ..)
意味着Frame(None, ..)
并且它添加到root
You have to do it in two steps你必须分两步完成
inner_frame = Frame(...)
inner_frame.grid(...)
And now you have Frame
assigned to inner_frame
现在您已将
Frame
分配给inner_frame
EDIT:编辑:
With correctly assigned widgets I get使用正确分配的小部件,我得到
and now gray box is inside yellow frame but image shows different problem - grid()
/ pack()
automatically calculate position and size and external frame automatically change size to fit to child's size.现在灰色框在黄色框架内,但图像显示不同的问题 -
grid()
/ pack()
自动计算位置和大小,外部框架自动更改大小以适合孩子的大小。
Using .grid_propagate(False)
you can stop it使用
.grid_propagate(False)
你可以停止它
But it shows other problem - cells don't use full size of parent so yellow frame is moved to left top corner, not centered :) Empty cells have width = 0
and heigh = 0
so moving all to next row and next column will not change it - it will be still in left top corner :)但它显示了其他问题 - 单元格不使用父级的完整大小,因此黄色框移到左上角,而不是居中:) 空单元格的
width = 0
和heigh = 0
因此不会全部移动到下一行和下一列改变它 - 它仍然在左上角:)
You need to assign weight
to column and/or row which decide how to use free space.您需要为决定如何使用可用空间的列和/或行分配
weight
。 All columns/rows has weight=0
and when you set weight=1
then this row and/or column will use all free space - (this would need better explanation) - and then element inside cell will be centered.所有列/行的
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()
BTW: PEP 8 -- Style Guide for Python Code顺便说一句: PEP 8 -- Python 代码风格指南
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.