[英]Tkinter make a frame fill whole column
我正在嘗試使用 Tkinter 模塊創建一個簡單的 GUI。 我有一個由兩列組成的布局(權重為 1 和 2)。 現在,我希望我添加的兩個小部件(cfg 和 cfgx)能夠填滿放置它們的整個列。 我怎么能用我目前的設置實現這樣的事情? 提前致謝
import tkinter as tk
from components.ConfigCreator import ConfigCreator
WIDTH = 800
HEIGHT = 600
POS_X = 300
POS_Y = 200
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
# root
self.parent = parent
self._setup_size_and_positioning()
self._setup_layout()
self._setup_widgets()
def _setup_size_and_positioning(self) -> None:
self.winfo_toplevel().title('Test app')
self.winfo_toplevel().geometry(f"{WIDTH}x{HEIGHT}+{POS_X}+{POS_Y}")
self.config(width=WIDTH, height=HEIGHT)
def _setup_layout(self) -> None:
self.grid(row=0, column=0)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=2)
def _setup_widgets(self) -> None:
cfg = ConfigCreator(self)
cfg.grid(row=0, column=0)
cfg.config(bg="limegreen")
cfgx = ConfigCreator(self)
cfgx.grid(row=0, column=1)
cfgx.config(bg="skyblue")
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root).pack(side="top", fill="both", expand=True)
root.mainloop()
@編輯
#ConfigCreator.py
import tkinter as tk
class ConfigCreator(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.