Tkinter——如何水平居中 canvas 文本?
Tkinter -- how to horizontally center canvas text?
我正在 UI class 中开发一个功能,它是一个配置 window,它显示程序的徽标并在底部有一个更新文本告诉你知道它在加载什么,等等。这是我目前所拥有的:
self.window = "config"
self.windowWidth = 340
self.windowHeight = 270
infoText = "Configuring Kh..."
self.root = tk.Tk()
self.root.geometry("%dx%d+400+400" % (self.windowWidth, self.windowHeight))
self.root.title("Kh Control v1.1 starting...")
logo = tk.PhotoImage(file="KhLogo.gif")
mainPanel = tk.Canvas(self.root, width=self.windowWidth, height=self.windowHeight)
mainPanel.image = logo
mainPanel.pack()
mainPanel.create_image(0, 0, image=logo, anchor="nw")
mainPanel.create_text(0,200, text=infoText, anchor="nw", fill="yellow")
return
我希望 infoText 中的文本水平居中,垂直向下偏移约 200 像素。垂直偏移效果很好,但我不知道如何将文本水平居中。
我开始尝试旧的 ((width / 2) - (str length / 2)) 但后来意识到每个字母不是 1px。并且 anchor = "center" 似乎只将一半的文本放在屏幕左侧。
我是 Python 的新手(现在才几天)所以如果我遗漏了一些明显的东西,这就是原因。
编辑:如果不是很明显,此文本会更改,所以我不能只对偏移量做出绝对决定,它必须随文本更改
您尝试过使用 justify
参数吗?
通常 center
与非文本对象一起使用。
可能会提供一些见解。
我在翻阅 canvas 参考资料后弄明白了。
canvas 有一个名为 bbox 的方法,它 returns 一个包含项目占据区域的 (x1, y1, x2, y2) 的元组。我得到了那些坐标,制定了一个函数来找到它的 px 长度,将它除以 2,然后从 window 宽度中减去它。然后我使用 canvas.move 使用函数返回的数字更改 x 偏移量。
def findXCenter(self, canvas, item):
coords = canvas.bbox(item)
xOffset = (self.windowWidth / 2) - ((coords[2] - coords[0]) / 2)
return xOffset
主要部分在这里:
textID = mainPanel.create_text(0,0, text=infoText, anchor="nw", fill="yellow")
xOffset = self.findXCenter(mainPanel, textID)
mainPanel.move(textID, xOffset, 0)
希望我搜索此答案的时间对以后的人有所帮助。
您可以使用 .create_text() 方法的第一部分设置文本的位置。
见 http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/create_text.html
要使此位置水平居中于 window 使用 self.windowWidth / 2 作为 x 坐标
默认情况下,文本锚定在给定位置的中心。
(.create_text 将默认为 anchor="CENTER")
您还需要删除锚点="nw",因为这会使您的文本显示在给定位置的下方和右侧
因此,您的更新代码应该是。
self.window = "config"
self.windowWidth = 340
self.windowHeight = 270
infoText = "Configuring Kh..."
self.root = tk.Tk()
self.root.geometry("%dx%d+400+400" % (self.windowWidth, self.windowHeight))
self.root.title("Kh Control v1.1 starting...")
logo = tk.PhotoImage(file="KhLogo.gif")
mainPanel = tk.Canvas(self.root, width=self.windowWidth, height=self.windowHeight)
mainPanel.image = logo
mainPanel.pack()
mainPanel.create_image(0, 0, image=logo, anchor="nw")
mainPanel.create_text(self.windowWidth/2,200, text=infoText, fill="yellow")
return
我正在 UI class 中开发一个功能,它是一个配置 window,它显示程序的徽标并在底部有一个更新文本告诉你知道它在加载什么,等等。这是我目前所拥有的:
self.window = "config"
self.windowWidth = 340
self.windowHeight = 270
infoText = "Configuring Kh..."
self.root = tk.Tk()
self.root.geometry("%dx%d+400+400" % (self.windowWidth, self.windowHeight))
self.root.title("Kh Control v1.1 starting...")
logo = tk.PhotoImage(file="KhLogo.gif")
mainPanel = tk.Canvas(self.root, width=self.windowWidth, height=self.windowHeight)
mainPanel.image = logo
mainPanel.pack()
mainPanel.create_image(0, 0, image=logo, anchor="nw")
mainPanel.create_text(0,200, text=infoText, anchor="nw", fill="yellow")
return
我希望 infoText 中的文本水平居中,垂直向下偏移约 200 像素。垂直偏移效果很好,但我不知道如何将文本水平居中。
我开始尝试旧的 ((width / 2) - (str length / 2)) 但后来意识到每个字母不是 1px。并且 anchor = "center" 似乎只将一半的文本放在屏幕左侧。
我是 Python 的新手(现在才几天)所以如果我遗漏了一些明显的东西,这就是原因。
编辑:如果不是很明显,此文本会更改,所以我不能只对偏移量做出绝对决定,它必须随文本更改
您尝试过使用 justify
参数吗?
通常 center
与非文本对象一起使用。
可能会提供一些见解。
我在翻阅 canvas 参考资料后弄明白了。
canvas 有一个名为 bbox 的方法,它 returns 一个包含项目占据区域的 (x1, y1, x2, y2) 的元组。我得到了那些坐标,制定了一个函数来找到它的 px 长度,将它除以 2,然后从 window 宽度中减去它。然后我使用 canvas.move 使用函数返回的数字更改 x 偏移量。
def findXCenter(self, canvas, item):
coords = canvas.bbox(item)
xOffset = (self.windowWidth / 2) - ((coords[2] - coords[0]) / 2)
return xOffset
主要部分在这里:
textID = mainPanel.create_text(0,0, text=infoText, anchor="nw", fill="yellow")
xOffset = self.findXCenter(mainPanel, textID)
mainPanel.move(textID, xOffset, 0)
希望我搜索此答案的时间对以后的人有所帮助。
您可以使用 .create_text() 方法的第一部分设置文本的位置。 见 http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/create_text.html
要使此位置水平居中于 window 使用 self.windowWidth / 2 作为 x 坐标
默认情况下,文本锚定在给定位置的中心。 (.create_text 将默认为 anchor="CENTER")
您还需要删除锚点="nw",因为这会使您的文本显示在给定位置的下方和右侧
因此,您的更新代码应该是。
self.window = "config"
self.windowWidth = 340
self.windowHeight = 270
infoText = "Configuring Kh..."
self.root = tk.Tk()
self.root.geometry("%dx%d+400+400" % (self.windowWidth, self.windowHeight))
self.root.title("Kh Control v1.1 starting...")
logo = tk.PhotoImage(file="KhLogo.gif")
mainPanel = tk.Canvas(self.root, width=self.windowWidth, height=self.windowHeight)
mainPanel.image = logo
mainPanel.pack()
mainPanel.create_image(0, 0, image=logo, anchor="nw")
mainPanel.create_text(self.windowWidth/2,200, text=infoText, fill="yellow")
return