如何在Tkinter中智能创建可调整大小的窗口与控件?
grid() Hey there! I totally get your frustration with place()—it’s great for one-off precise positioning, but it turns into a huge headache when you need elements to stay aligned and scale properly as the window resizes. Let’s ditch those messy relx/rely calculations and switch to grid() which is made exactly for structured layouts like your checkbox grid.
Why grid() is better here
Instead of manually calculating every widget’s position, grid() lets you organize elements into rows and columns. You can set column weights to make columns expand evenly when the window grows, and use the sticky parameter to keep widgets aligned perfectly without guesswork.
Here’s your refactored code with grid()
This version keeps all your checkboxes, aligns them neatly, and scales smoothly when you resize the window:
import tkinter as tk root = tk.Tk() root.geometry("650x400+400+50") root.minsize(650, 400) # Configure columns to expand evenly when window resizes for col in range(4): root.columnconfigure(col, weight=1) # --- Column 1 --- KLval = tk.BooleanVar() KL = tk.Checkbutton(root, text="Knots and Lashings", variable=KLval) KL.grid(row=1, column=0, sticky="w", padx=10, pady=5) SUval = tk.BooleanVar() SU = tk.Checkbutton(root, text="Sense of Urgency", variable=SUval) SU.grid(row=2, column=0, sticky="w", padx=10, pady=5) FCval = tk.BooleanVar() FC = tk.Checkbutton(root, text="Fieldcraft", variable=FCval) FC.grid(row=3, column=0, sticky="w", padx=10, pady=5) STval = tk.BooleanVar() ST = tk.Checkbutton(root, text="Stealth", variable=STval) ST.grid(row=4, column=0, sticky="w", padx=10, pady=5) # --- Column 2 --- PLval = tk.BooleanVar() PL = tk.Checkbutton(root, text="Planning", variable=PLval) PL.grid(row=1, column=1, sticky="w", padx=10, pady=5) RAval = tk.BooleanVar() RA = tk.Checkbutton(root, text="Radios", variable=RAval) RA.grid(row=2, column=1, sticky="w", padx=10, pady=5) COval = tk.BooleanVar() CO = tk.Checkbutton(root, text="Communication", variable=COval) CO.grid(row=3, column=1, sticky="w", padx=10, pady=5) SGval = tk.BooleanVar() SG = tk.Checkbutton(root, text="Strategy", variable=SGval) SG.grid(row=4, column=1, sticky="w", padx=10, pady=5) # --- Column 3 --- PSval = tk.BooleanVar() PS = tk.Checkbutton(root, text="Problem Solving", variable=PSval) PS.grid(row=0, column=2, sticky="w", padx=10, pady=5) DEval = tk.BooleanVar() DE = tk.Checkbutton(root, text="Decoding", variable=DEval) DE.grid(row=1, column=2, sticky="w", padx=10, pady=5) FAval = tk.BooleanVar() FA = tk.Checkbutton(root, text="First Aid", variable=FAval) FA.grid(row=2, column=2, sticky="w", padx=10, pady=5) PRval = tk.BooleanVar() PR = tk.Checkbutton(root, text="Prioritising", variable=PRval) PR.grid(row=3, column=2, sticky="w", padx=10, pady=5) DMval = tk.BooleanVar() DM = tk.Checkbutton(root, text="Decision Making", variable=DMval) DM.grid(row=4, column=2, sticky="w", padx=10, pady=5) # --- Column 4 --- REval = tk.BooleanVar() RE = tk.Checkbutton(root, text="Re-Evaluation", variable=REval) RE.grid(row=0, column=3, sticky="w", padx=10, pady=5) MCval = tk.BooleanVar() MC = tk.Checkbutton(root, text="Management and Control", variable=MCval) MC.grid(row=1, column=3, sticky="w", padx=10, pady=5) CRval = tk.BooleanVar() CR = tk.Checkbutton(root, text="Creativity", variable=CRval) CR.grid(row=2, column=3, sticky="w", padx=10, pady=5) CKval = tk.BooleanVar() CK = tk.Checkbutton(root, text="Core Knowledge", variable=CKval) CK.grid(row=3, column=3, sticky="w", padx=10, pady=5) # Add a spacer row at the bottom to push checkboxes up root.rowconfigure(5, weight=1) root.mainloop()
Key improvements explained
- Column weights: The loop
for col in range(4): root.columnconfigure(col, weight=1)makes all 4 columns expand equally when you resize the window, so your checkboxes stay evenly spaced. - Sticky alignment:
sticky="w"ensures every checkbox is left-aligned within its column—no more manualrelxtweaks to fix misalignment. - Spacing:
padxandpadyadd consistent gaps between widgets, keeping the layout clean without having to calculaterelyvalues. - Spacer row:
root.rowconfigure(5, weight=1)adds a flexible row at the bottom that expands when the window grows, pushing all checkboxes up instead of stretching them.
Bonus tip
If you want checkboxes to fill the width of their columns (instead of staying left-aligned), you can change sticky="w" to sticky="we" (west-east). This makes each checkbox expand horizontally with the column.
内容的提问来源于stack exchange,提问作者Holly

