无法使用tkinter中的justify和anchor命令控制标签中文本的位置。

2024-09-28 21:57:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法控制标签的文本。我正在使用位置来定位这些标签

下面是我的示例代码

from tkinter import *

root = Tk()
root.geometry('1080x640+0+0')


Headings = ['Months','Days','* Occupancy','Energy \nConsumption','Fuel \nConsumption', 'Specific Fuel \nConsumption','Diesel Price','Specific Energy \nConsumption']
Units = ['2017','per month','man days/month','kWh/month','Litres','Litres/kWh','AED','kWh/man/day']
Headings_wd = [0,0,0,0,0,0,0,0]
Units_wd = [0,0,0,0,0,0,0,0]

for a in range(0,len(Headings)):
    Headings_wd[a] = Label(root, text = Headings[a], justify = LEFT )
    Headings_wd[a].place(x = 20 + a * 100, y = 20)
    Units_wd[a] = Label(root, text = Units[a], anchor = 'e' , font = ("Helvetica" , 6) )
    Units_wd[a].place(x = 20 + a * 100, y = 60)

root.mainloop()

我想在标签的最右边有labelsUnits_wd 我该怎么办。你知道吗

我曾经尝试过在这个站点上使用命令Units_wd[a].config(borderwidth=1, relief="solid")进行调试,但是当我使用place时,这些命令不起作用。你知道吗


Tags: placeroot标签labelenergykwhunitswd
1条回答
网友
1楼 · 发布于 2024-09-28 21:57:29

如注释中所述,通过使用width命令来获得所需的结果,可以解决问题

Headings_wd[a] = Label(root, text = Headings[a], width = 10, anchor = 'e')

Units_wd[a] = Label(root, text = Units[a], width = 10, anchor = 'e' , font = ("Helvetica" , 6) )

谢谢@什么意思

相关问题 更多 >