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
[Tkinter] How do I center this text in tkinter?
[Tkinter] How do I center this text in tkinter?
|
Mar-29-2019, 05:57 PM
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() (Mar-29-2019, 05:57 PM)Larz60+ Wrote: This works with pack geometry 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
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
Mar-29-2019, 10:35 PM
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 |
|
Users browsing this thread: 2 Guest(s)