如何调整Tkinter中LabelFrame的标题至按钮右侧?
Nice question! Tkinter’s default LabelFrame locks its title centered at the top, but there are two practical ways to shift it to the right of your button:
Method 1: Adjust LabelFrame’s labelanchor Property
Tkinter’s LabelFrame has a built-in labelanchor parameter that controls where the title sits relative to the frame’s border. Setting it to 'e' (east) will place the title on the right edge of the frame’s border. Pair this with making your button left-aligned inside the frame for the desired visual effect:
"""Button frames""" # Add labelanchor='e' to move title to right border constituent_frame = LabelFrame(root, text="Fibre and resin data inputs", padx=2, pady=2, labelanchor='e') ply_frame = LabelFrame(root, text="Single lamina data inputs", padx=2, pady=2, labelanchor='e') laminate_frame = LabelFrame(root, text="Complete ply stack data inputs", padx=2, pady=2, labelanchor='e') """Frame positions""" constituent_frame.grid(row=2, column=1, padx=50) ply_frame.grid(row=3, column=1, padx=50) laminate_frame.grid(row=4, column=1, padx=50) """Button definitions""" constituent_button = Button(constituent_frame, text='Constituent', width=15, command=click_constituent) ply_button = Button(ply_frame, text='Ply', width=15, command=click_ply) laminate_button = Button(laminate_frame, text='Laminate', width=15, command=click_laminate) """Button positions""" # Keep buttons left-aligned in the frame constituent_button.grid(row=2, column=1, pady=2, sticky='w') ply_button.grid(row=3, column=1, pady=2, sticky='w') laminate_button.grid(row=4, column=1, pady=2, sticky='w')
This is a quick fix using native Tkinter properties, though the title will sit on the frame’s border rather than directly next to the button.
Method 2: Use a Regular Frame with Custom Button + Title Layout
For full control over the title’s position (so it sits directly beside the button on the same line), replace the LabelFrame with a standard Frame and add a separate Label for the title. This mimics the LabelFrame border style while letting you fine-tune spacing:
"""Button frames (using regular Frame instead of LabelFrame)""" # Add bd=2 and relief='groove' to match LabelFrame's border look constituent_frame = Frame(root, padx=2, pady=2, bd=2, relief='groove') ply_frame = Frame(root, padx=2, pady=2, bd=2, relief='groove') laminate_frame = Frame(root, padx=2, pady=2, bd=2, relief='groove') """Frame positions""" constituent_frame.grid(row=2, column=1, padx=50) ply_frame.grid(row=3, column=1, padx=50) laminate_frame.grid(row=4, column=1, padx=50) """Button and Title Layout""" # Constituent section constituent_button = Button(constituent_frame, text='Constituent', width=15, command=click_constituent) constituent_button.grid(row=0, column=0, pady=2) constituent_title = Label(constituent_frame, text="Fibre and resin data inputs") constituent_title.grid(row=0, column=1, padx=10, pady=2) # Ply section ply_button = Button(ply_frame, text='Ply', width=15, command=click_ply) ply_button.grid(row=0, column=0, pady=2) ply_title = Label(ply_frame, text="Single lamina data inputs") ply_title.grid(row=0, column=1, padx=10, pady=2) # Laminate section laminate_button = Button(laminate_frame, text='Laminate', width=15, command=click_laminate) laminate_button.grid(row=0, column=0, pady=2) laminate_title = Label(laminate_frame, text="Complete ply stack data inputs") laminate_title.grid(row=0, column=1, padx=10, pady=2)
This approach lets you adjust the padx value to get exactly the spacing you want between the button and title, and keeps them perfectly aligned horizontally.
内容的提问来源于stack exchange,提问作者Joseph Joe Soltan

