简体   繁体   English

中心 label 在 canvas tkinter python 中使用 place()

[英]center label using place() in canvas tkinter python

I am trying to place label at the center using place() but when the text is changed its alignment shift to the right because of length of new word.我正在尝试使用place()将 label 放在中心,但是当文本更改时,由于新单词的长度,它的 alignment 向右移动。 How do I fix this using place() .如何使用place()解决此问题。

from tkinter import *

BACKGROUND_COLOR = "#B1DDC6"

# ------------------------------ UI --------------------------------------#
window = Tk()
window.title('Flashy')
window.config(padx = 50, pady = 50,bg=BACKGROUND_COLOR )



canvas = Canvas(window, width= 800, height = 626,bg = BACKGROUND_COLOR,highlightthickness=0)
canvas.grid(row  = 0, column = 0, columnspan = 2)
card_front_img = PhotoImage(file="./images/card_front.png")
canvas.create_image(400,263,image = card_front_img)
lang_label = Label(text   = 'French', font = ('Ariel',40,'italic'),anchor = CENTER)
lang_label.place(x = 300, y = 100)
word_label =Label(text = 'trouva', font = ('Arial',60,'bold'),anchor= CENTER)
word_label.place(x = 250, y = 200 )
window.mainloop()

Before:前:

word_label =Label(text = 'trouve', font = ('Arial',60,'bold'),anchor= CENTER)
word_label.place(x = 250, y = 200 )

在此处输入图像描述

After:后:

word_label =Label(text = 'trodawdauve', font = ('Arial',60,'bold'),anchor= CENTER)
word_label.place(x = 250, y = 200 )

在此处输入图像描述

I found an alternative which works fine but doesn't use the place() .By using canvas.create_text() instead of place() the text aligns itself in the centre irrespective of the length of the word我找到了一个可以正常工作但不使用place()的替代方法。通过使用canvas.create_text()而不是place()文本在中心对齐,而与单词的长度无关

canvas.create_text(400,150, text='French', font=('Ariel', 40, 'italic'))
canvas.create_text(400,263, text='wordwdawdadawd', font=('Ariel', 60, 'bold'))

If you want to use place , you simply need to set the anchor attribute to "n" (north).如果要使用place ,只需将anchor属性设置为"n" (北)。 That will cause the "north" (top-center) portion of the widget to be at the specified coordinates.这将导致小部件的“北”(顶部中心)部分位于指定的坐标处。 Using "s" (south) or "c" (center) would give a similar effect, though it changes which part of the label is at the given y coordinate.使用"s" (南)或"c" (中)会产生类似的效果,尽管它会改变 label 的哪一部分在给定的 y 坐标处。

lang_label.place(x = 250, y = 100, anchor="n")
word_label.place(x = 250, y = 200, anchor="n" )

No matter how long the text in world_label is, it will always be centered at the exact same point as lang_label .无论world_label中的文本有多长,它总是以与lang_label完全相同的点为中心。

Note: the x and y coordinates are just for illustrative purposes, you'll want to compute them to be the appropriate value for where you want them to be.注意:x 和 y 坐标仅用于说明目的,您需要将它们计算为您希望它们所在位置的适当值。 The point being, if the x coordinate is the same, the labels will be centered with respect to each other.关键是,如果 x 坐标相同,则标签将相对于彼此居中。

You can also create a frame that's centered and then place the label onto it.您还可以创建一个居中的框架,然后将 label 放在上面。 Here's a great website that really helped me learn thinker: https://www.tutorialspoint.com/python/python_gui_programming.htm这是一个真正帮助我学习思想家的好网站: https://www.tutorialspoint.com/python/python_gui_programming.htm

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

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