Parallax Sugar
Parallax Sugar

Reputation: 705

How can I center all text, using the tkinter grid method?

I'm trying to create a ticket (in tkinter) that mimics the ticket you would receive from a ticket machine if you had just paid to enter the Durham Gardens. Line by line, it should have a welcome message, followed by the price of entry, then the registration, and finally the goodbye message.

The problem I'm having is making all the widgets (which are labels) centred so that the ticket looks asthetically pleasing.

So far the 'Registration' and its corresponding value are aligned horizontally, as are the 'Price of entry' and its corresponding value. These things are aligned one above the other. However, I could not find a way to maintain the latter whilst still keeping the welcome message and the goodbye message in the centre.

So at the moment, the welcome and goodbye messages are much too far to the right from the rest of the values, and although the 'Price of entry' and 'Registration' do match their values horizontally, there is a large gap between them that I haven't found a way to get rid of.

from tkinter import *

TicketPrice = 25    # for the labelPrice below
WrittenPrice = "£ " + str(TicketPrice)
reg = "SMI" # for the labelREG below


root = Tk()
root.title("Your Ticket")
root.geometry("300x150")

labelWelcome = Label(root, text="The Durham Gardens")
labelBye = Label(root, text="Thank you for visiting. Enjoy!") 

labelPrice = Label(root, text="Price of Entry") # no frame
labelREG = Label(root, text="Registration")
ActualPrice = Label(root, text=WrittenPrice) # get ticekt price
ActualREG = Label(root, text=str(reg)) 

root.grid_rowconfigure(0, minsize=20)
root.grid_rowconfigure(2, minsize=20) 
root.grid_rowconfigure(6, minsize=20) 
root.grid_columnconfigure(0, minsize=100) 



labelPrice.grid(row=3, sticky=E) 
labelREG.grid(row=5, sticky=E) 

ActualPrice.grid(row=3, column=1)
ActualREG.grid(row=5, column=1)  


labelWelcome.grid(row=1, column=1, ipadx=50, sticky=E) # column
labelBye.grid(row=7, column=1, ipadx=50, sticky=E) 

root.mainloop() 

I'm basically trying to achieve a similar output on tkinter to what you'd get in the shell if you ran this:

print("*"*40)
print("*".ljust(39) + "*")
print("*".ljust(10) + "The Durham Gardens" + "*".rjust(12))
print("*".ljust(9) + "Price of Entry: £" + ) + "*".rjust(12))
print("*".ljust(15) + "REG: " + str(reg) + "*".rjust(17))
print("*".ljust(39) + "*")
print("*".ljust(5) + "Thank you for visiting. Enjoy!" + "*".rjust(5))
print("*".ljust(39) + "*")
print("*"*40)

So if you run the above, then that's what I mean when I say I want everything to look 'centred'.

I'm using the grid method at the moment because it was easier that way to align the 'Registration' and 'Price of Entry' labels with their value.

All the widgets are labels, consisting of only text. Is there a way to put everything in the centre?

Upvotes: 0

Views: 3904

Answers (1)

Tadhg McDonald-Jensen
Tadhg McDonald-Jensen

Reputation: 21474

as @jasonharper stated, the rows that only have one thing in them need columnspan=2 specified to span both columns (and start at column=0)

labelWelcome.grid(row=1, column=0, columnspan=2, ipadx=50) # column
labelBye.grid(row=7, column=0,columnspan=2, ipadx=50) 

Upvotes: 1

Related Questions