Python Forum
[Tkinter] How do I center this text in tkinter?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How do I center this text in tkinter?
#1
from tkinter import *

window = Tk()
window.title("List")
window.geometry("700x450")
window.configure(bg="orange red")

#center this label
Label (window, text="List", bg="orange red", fg="white", font="none 24 bold").grid(row=0, column=0)


Label (window, text="Enter something here:", bg="orange red", fg="white", font="none 12 bold").grid(row=1, column=0, sticky=W)


window.mainloop()
I want it to be in the center regardless of the size of the window. So if someone resizes the window, it stays in the center
Reply
#2
This works with pack geometry
from tkinter import *
 
window = Tk()
window.title("List")
window.geometry("700x450")
window.configure(bg="orange red")
 
#center this label
lbl1 = Label(window, text="List", bg="orange red", fg="white", font="none 24 bold")
lbl1.config(anchor=CENTER)
lbl1.pack()
 
 
lbl2 = Label (window, text="Enter something here:", bg="orange red", fg="white", font="none 12 bold")
lbl2.config(anchor=CENTER)
lbl2.pack()

window.mainloop()
Reply
#3
(Mar-29-2019, 05:57 PM)Larz60+ Wrote: This works with pack geometry
from tkinter import *
 
window = Tk()
window.title("List")
window.geometry("700x450")
window.configure(bg="orange red")
 
#center this label
lbl1 = Label(window, text="List", bg="orange red", fg="white", font="none 24 bold")
lbl1.config(anchor=CENTER)
lbl1.pack()
 
 
lbl2 = Label (window, text="Enter something here:", bg="orange red", fg="white", font="none 12 bold")
lbl2.config(anchor=CENTER)
lbl2.pack()

window.mainloop()

Tyvm, but how do I make lbl2 be on the left side of the window now? I tried putting it in .pack(side=LEFT) but it shows up in the middle of the screen and not underneath the "List" text
Reply
#4
You would think that removing line 15 would do this.
This one of the reasons why I don't like tkinter as opposed to wxpython.
But tkinter will not do this unless you set width of the label
so, (actually you should change both labels, and this can actually be done inside the label definition)

from tkinter import *
 
window = Tk()
window.title("List")
lblwidth = 700
window.geometry("{}x450".format(lblwidth))
window.configure(bg="orange red")
 
# center this label
lbl1 = Label(window, text="List", bg="orange red", fg="white", font="none 24 bold", 
    width=lblwidth, anchor=CENTER)
lbl1.pack()

lbl2 = Label (window, text="Enter something here:", bg="orange red", 
    fg="white", font="none 12 bold", width=lblwidth, anchor=W)
lbl2.pack()
 
window.mainloop()
Anchor values:
   

Get yourself a tkinter manual here: http://www.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf
Reply
#5
To center the text on the label use anchor=center http://effbot.org/tkinterbook/label.htm grid() defaults to centering the label in the row and column you grid(). Use sticky when you don't want the label centered
https://effbot.org/tkinterbook/grid.htm
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Updating tkinter text BliepMonster 5 5,672 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,672 Jun-26-2022, 06:26 PM
Last Post: menator01
  tkinter change the text of the checkbox zazas321 1 3,759 Sep-17-2021, 06:19 AM
Last Post: zazas321
  tkinter text widget word wrap position chrisdb 6 7,462 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,546 Mar-10-2021, 04:21 PM
Last Post: Sir
  [PyGTK] How to center text on multi-line buttons? Lomax 3 4,159 Jan-23-2021, 03:23 PM
Last Post: Lomax
Photo Tkinter TEXT background image _ShevaKadu 5 7,656 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  tkinter | Button color text on Click Maryan 2 3,317 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [Tkinter] Text Upload LearningLittlebyLittle 0 2,014 Sep-04-2020, 07:55 PM
Last Post: LearningLittlebyLittle
  [Tkinter] Indentation for Tkinter-made Text Editor ShakeyPakey 4 5,053 Jun-08-2020, 03:13 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020