Tkinter中使用grid()布局实现Frame内按钮对齐及Frame宽度属性作用的疑问
Let's work through your two problems step by step to get your scratchpad looking and behaving exactly how you want it.
1. Aligning the "E" (Clear) Button to the Right
The reason sticky=tk.E isn't working for your bottombuttone is that your bottom frame's grid layout doesn't have any space to let the button stick to the right. Right now, all columns in the bottom frame only take up as much width as the buttons inside them—there's no column set to expand and fill the available space, which would push the E button to the edge.
Here's the fix:
- Add a "spacer" column between your formatting buttons (A-D) and the E button. We'll set this column to absorb all extra width in the
bottomframe. - Move the E button to a higher column number, then configure the spacer column with
weight=1so it stretches when the window resizes.
Update your button grid code and add the column configuration like this:
# Layout formatting buttons on the left bottombuttona.grid(column=0, row=0, ipady=5) bottombuttonb.grid(column=1, row=0, ipady=5) bottombuttonc.grid(column=2, row=0, ipady=5) bottombuttond.grid(column=3, row=0, ipady=5) # Spacer column that takes all extra width to push E right bottom.grid_columnconfigure(4, weight=1) # Align E button to the right in column 5 bottombuttone.grid(column=5, row=0, ipady=5, sticky=tk.E)
Also, note you had row=2 for the bottom buttons—since they're inside the bottom frame, their row should be 0 (each frame has its own independent grid context separate from the root frame).
2. Why the width Option Isn't Working for ttk.Frame
The width=2000 you set on your bottom frame doesn't affect ttk.Frame widgets because ttk widgets rely on styles for sizing, not direct width/height parameters (those are for classic tk widgets like tk.Frame).
For ttk.Frame, its size is determined by:
- The size of its child widgets (by default, it shrinks to fit them)
- Explicit minimum size set with
.minsize(width, height) - Parent layout configurations that force the frame to stretch (using
grid_columnconfigure/grid_rowconfigureon the parent)
To make your bottom frame stretch across the full window width, you already have sticky=tk.NSEW on its grid call—you just need to make sure the root frame's column is set to expand too. Add these lines right after root.grid():
root.grid_columnconfigure(0, weight=1) root.grid_rowconfigure(1, weight=1) # Also makes the Text widget stretch vertically
Full Modified Code
Here's the complete fixed code with all changes applied:
import tkinter as tk from tkinter import ttk wn = tk.Tk() wn.attributes('-type', 'splash') style = ttk.Style() style.configure('top.TFrame', border=0, borderwidth=0, background='green') style.configure('bottom.TFrame', border=0, borderwidth=0, background='blue') style.configure('button.TButton', border=5, borderwidth=5, background='#e6e497', bd=5) root = ttk.Frame(wn, style='top.TFrame', borderwidth=5, relief='ridge') button = ttk.Button(root, text='X', width=3, command=lambda: wn.destroy(), style='button.TButton') txt = tk.Text(root, width=40, background='light yellow', bd=5) bottom = ttk.Frame(root, style='bottom.TFrame', borderwidth=5, relief='ridge') bottombuttona = ttk.Button(bottom, width=3, text='A') bottombuttonb = ttk.Button(bottom, width=3, text='B') bottombuttonc = ttk.Button(bottom, width=3, text='C') bottombuttond = ttk.Button(bottom, width=3, text='D') bottombuttone = ttk.Button(bottom, width=3, text='E') # Configure root frame to stretch with window root.grid(sticky=tk.NSEW) root.grid_columnconfigure(0, weight=1) root.grid_rowconfigure(1, weight=1) button.grid(column=0, row=0, sticky=tk.E, ipady=4) txt.grid(column=0, row=1, sticky=tk.NSEW) bottom.grid(column=0, row=2, sticky=tk.NSEW) # Layout bottom buttons bottombuttona.grid(column=0, row=0, ipady=5) bottombuttonb.grid(column=1, row=0, ipady=5) bottombuttonc.grid(column=2, row=0, ipady=5) bottombuttond.grid(column=3, row=0, ipady=5) # Spacer column to push E button right bottom.grid_columnconfigure(4, weight=1) bottombuttone.grid(column=5, row=0, ipady=5, sticky=tk.E) txt.focus_force() wn.bind('<Escape>', lambda x: wn.destroy()) wn.mainloop()
Now your E button will stay aligned to the right of the blue frame, the Text widget will stretch vertically with the window, and the bottom frame will span the full width of the app.
内容的提问来源于stack exchange,提问作者yuser099881232




