简体   繁体   中英

How to I change text position on a frame in Tkinter

Rather a specific request I can't seem to find any online documentation on:

I'm trying to change the position of the font on frames surrounding buttons. As shown below the text is currently centered above the button. I would like the text to be displayed to the right of the button.

在此处输入图像描述

Heres my code for the frames and buttons:

"""Button frames"""
constituent_frame = LabelFrame(root, text="Fibre and resin data inputs", padx=2, pady=2)
ply_frame = LabelFrame(root, text="Single lamina data inputs", padx=2, pady=2)
laminate_frame = LabelFrame(root, text="Complete ply stack data inputs", padx=2, pady=2)

"""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"""
constituent_button.grid(row=2, column=1, pady=2)
ply_button.grid(row=3, column=1, pady=2)
laminate_button.grid(row=4, column=1, pady=2)

Any advice or links to resources would be greatly appreciated.

You can use the labelanchor argument on the LabelFrame class to achieve this:

from tkinter import *

root = Tk()

labelframe = LabelFrame(root, text="Hello", labelanchor=E)
button = Button(labelframe, text="BUTTON")
labelframe.pack()
button.pack()

root.mainloop()

possible values are the cardinal directions (NSEW) - tkinter has constants for these

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM