简体   繁体   中英

How to center text content in tkinter label

I'm trying to make a very basic game where you can move around on a 2D plane made out of normal characters. However I can't seem to get the "world"-text to be centralized in the main label.

I'm thinking there might be some hidden empty spaces but I've not managed to find anymore at this point. Especially on the right side there seems to be an empty column or something?

Any help will be very appreciated and please bear with my code. Cheers!

from tkinter import *
import tkinter as tk

coordinates = {
    "x1y1": "#", "x1y2": "#", "x1y3": "#", "x1y4": "#", "x1y5": "#", "x1y6": "#", "x1y7": "#", "x1y8": "#", "x1y9": "#", "x1y10": "#",
    "x2y1": "#", "x2y2": "#", "x2y3": "#", "x2y4": "#", "x2y5": "#", "x2y6": "#", "x2y7": "#", "x2y8": "#", "x2y9": "#", "x2y10": "#",
    "x3y1": "#", "x3y2": "#", "x3y3": "#", "x3y4": "#", "x3y5": "#", "x3y6": "#", "x3y7": "#", "x3y8": "#", "x3y9": "#", "x3y10": "#",
    "x4y1": "#", "x4y2": "#", "x4y3": "#", "x4y4": "#", "x4y5": "#", "x4y6": "#", "x4y7": "#", "x4y8": "#", "x4y9": "#", "x4y10": "#",
    "x5y1": "#", "x5y2": "#", "x5y3": "#", "x5y4": "#", "x5y5": "#", "x5y6": "#", "x5y7": "#", "x5y8": "#", "x5y9": "#", "x5y10": "#",
    "x6y1": "#", "x6y2": "#", "x6y3": "#", "x6y4": "#", "x6y5": "#", "x6y6": "#", "x6y7": "#", "x6y8": "#", "x6y9": "#", "x6y10": "#",
    "x7y1": "#", "x7y2": "#", "x7y3": "#", "x7y4": "#", "x7y5": "#", "x7y6": "#", "x7y7": "#", "x7y8": "#", "x7y9": "#", "x7y10": "#",
    "x8y1": "#", "x8y2": "#", "x8y3": "#", "x8y4": "#", "x8y5": "#", "x8y6": "#", "x8y7": "#", "x8y8": "#", "x8y9": "#", "x8y10": "#",
    "x9y1": "#", "x9y2": "#", "x9y3": "#", "x9y4": "#", "x9y5": "#", "x9y6": "#", "x9y7": "#", "x9y8": "#", "x9y9": "#", "x9y10": "#",
    "x10y1":"#","x10y2": "#", "x10y3":"#", "x10y4":"#", "x10y5":"#", "x10y6":"#", "x10y7":"#", "x10y8":"#", "x10y9":"#", "x10y10":"#",
}

#Player starting position
player_x = 5
player_y = 5


def update_world():

    #Create string with player coordinates and input into dictionary
    player_xy = "x", player_x, "y", player_y
    player_coord = ''.join(map(str, player_xy))
    coordinates[player_coord] = '€'

    #Update previous player tile to map tile
    coord_keys = coordinates.keys()
    for coords in coord_keys:
        if coords != player_coord:
            coordinates[coords] = ' '

    #Print map
    width = 10
    coord_values = coordinates.values()
    i=0
    world=""

    for item in coord_values:
        world = world + item + " "
        i += 1
        if i % width == 0:
            world = world + '\n'
        if i == 100:
            world = world[:-1]

    #refresh the label that displays the world
    world_label.place(y=90, x=1)
    world_label.configure(font=("Courier", 24), text=world, bg="AliceBlue", anchor="center", width=20, height=10)


root = Tk() #Create window
root.configure(background="white")


world_label = Label() #Create World Label widget
world_label.pack()

update_world() #Function update_world


root.geometry("1200x800") #Size of window 
root.title('Game 0.1') #Title of window 
root.mainloop() #

There is extra space at the end of each line added by the following code:

for item in coord_values:
    world = world + item + " "
    ...

To remove the extra space, modify the for loop as below:

for item in coord_values:
    world = world + item + " "
    i += 1
    if i % width == 0:
        world = world[:-1] + '\n'   # use world[:-1] instead of world
world = world[:-1]

However I would suggest to modify the for loop as below:

world = []
for i in range(0, 100, 10):
    world.append(' '.join(coord_values[i:i+10]))
world = '\n'.join(world)
# one-liner for the above:
# world = '\n'.join(' '.join(coord_values[i:i+10]) for i in range(0, 100, 10))

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