简体   繁体   中英

Tkinter Label center text set text to start from top left

I'm creating a GUI for my application with Tkinter, and when I create a Label, the text is always at the center of the label, is there a way to set the text to start from the top left?

big_text_area = Label(self.master, text='Hello, World!', width=70, height=25, bg='white', foreground='black')
big_text_area.place(x=380, y=30)

Example:

在此处输入图像描述

I believe the position of your label is determined by the .place() function. So if you want the label to be at the top left, you should do:

big_text_area.place(x = 0, y = 0)

Tutorialspoint and effbot have a documentation for the .place( ) function and the arguments it takes. Most have default values so keep that in mind.

Another thing I would point out is that the width and height keyword arguments do not change the size of your text. Rather, they change the rectangular box around your text. This might be the reason why your text is still in the middle even if you set its positions to the top left. effbot again has the details on the Label widget.

Just a quick working example: (Python 3.7)

from tkinter import *

master = Tk()

master.geometry("200x200") 

# to change text size, use font=("font name", size)
w = Label(master, text="Hello, world!", font=("Helvetica", 22))

# explicitly set the text to be at the top left corner
w.place(anchor = NW, x = 0, y = 0)

mainloop()

Font question is also answered in this post: How do I change the text size in a Label widget?

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