简体   繁体   English

如何在 tkinter label 中居中文本内容

[英]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.我正在尝试制作一个非常基本的游戏,您可以在其中移动由普通角色组成的 2D 平面。 However I can't seem to get the "world"-text to be centralized in the main label.但是,我似乎无法将“世界”文本集中在主要的 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 循环,如下所示:

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:但是我建议修改for循环如下:

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM