下面的代碼按預期作業。
import tkinter as tk
from tkinter import *
windowBackgroundColor = "#333333"
cardColor = "#000000"
textColor = "#ffffff"
class SystemInfoView():
def __init__(self, root):
self.root = root
def initializeView(self):
self.frameSystemInfo = tk.Frame(self.root, bg=cardColor)
self.frameSystemInfo.grid(column=0, row=0, rowspan=2, sticky="NSEW", padx=5, pady=5)
# self.frameSystemInfo.columnconfigure(0, weight=1)
# self.frameSystemInfo.rowconfigure(0, weight=1)
# self.frameSystemInfo.rowconfigure(1, weight=1)
# labelSystemTime = tk.Label(self.frameSystemInfo, text="System Time:")
# labelSystemTime.grid(column=0, row=0, sticky="NSE")
# entrySystemTime = tk.Entry(self.frameSystemInfo, font=("Helvetica", 14))
# entrySystemTime.grid(column=0, row=1, sticky="NSE")
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("My Title")
self.configure(bg=windowBackgroundColor)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(2, weight=1)
self.columnconfigure(3, weight=1)
self.rowconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.rowconfigure(2, weight=1)
self.systemInfoView = SystemInfoView(self)
self.systemInfoView.initializeView()
rectangle_2 = tk.Frame(self, bg="#000000")
rectangle_2.grid(column=1, row=0, sticky="NSEW", padx=5, pady=5)
rectangle_3 = tk.Frame(self, bg="#000000")
rectangle_3.grid(column=2, row=0, sticky="NSEW", padx=5, pady=5)
rectangle_4 = tk.Frame(self, bg="#000000")
rectangle_4.grid(column=3, row=0, sticky="NSEW", padx=5, pady=5)
app = App()
app.mainloop()
當我運行腳本時,我可以得到下面的結果。

我想做類似下面的東西。

頂部的標簽,標簽下的條目,條目下方的 3 個并排按鈕 這些應該在frameSystemInfo框架內。
當我取消注釋initializeView函式內部的代碼時,一切都會崩潰。

我該做什么?
uj5u.com熱心網友回復:
你只需要把小部件放在你想要的地方。在注釋掉的代碼中,您只將小部件添加到幾行,并且您的使用sticky與您想要的輸出不對應。
你的第二張圖片意味著至少有 5 行,第 5 行是空的,占據了框架底部的所有未使用的空間。您似乎還需要 4 列。您的標簽在第一行,您的條目在第二行,兩者都跨越所有列。
首先,您可能只想確保所有列的寬度相等,您可以使用該uniform選項來做到這一點。對于行,我猜您只想要最后一個空行來獲取所有額外空間,因此它應該是唯一具有非零的行weight。這將強制所有其他行位于頂部。
self.frameSystemInfo.columnconfigure((0,1,2,3), uniform="equal", weight=1)
self.frameSystemInfo.rowconfigure(4, weight=1)
接下來,只需將標簽放在第一行,將條目放在第二行,并使它們跨越此框架中的所有列。他們需要堅持牢房的兩側,以便他們使用分配給他們的所有空間。
labelSystemTime.grid(column=0, row=0, columnspan=4, sticky="nsew")
entrySystemTime.grid(column=0, row=1, columnspan=4, sticky="nsew")
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/323798.html
上一篇:If陳述句取決于.txt檔案的內容-Python3.9
下一篇:匹配分數的regex運算式
