簡體   English   中英

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

[英]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 = 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 代碼風格指南

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM