0

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:

enter image description here

  • Read up on [Tkinter.Label.config-method - options `anchor=`, `compound=`, `justify=`](http://effbot.org/tkinterbook/label.htm#Tkinter.Label.config-method) – stovfl May 10 '20 at 18:21
  • @stovfl Thanks! I solved it by setting `anchor=NW` in the `config` function of the `Label` –  May 10 '20 at 18:25
  • Here's a [working link](https://web.archive.org/web/20201112020335id_/http://effbot.org/tkinterbook/label.htm#Tkinter.Label.config-method) to replace the one in @stovfl's [comment](https://stackoverflow.com/questions/61715190/tkinter-label-center-text-set-text-to-start-from-top-left#comment109166988_61715190). – martineau Jun 06 '22 at 11:17

1 Answers1

1

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
E-L
  • 51
  • 5
  • I have the main window, and I have the `big_text_area` `Label` that I want to place it in the positions I specify in the `place` function, it is still not working. –  May 10 '20 at 18:22
  • Looks like you figured it out. Feel free to follow-up if you need to. – E-L May 10 '20 at 20:07