我想要一个 Python 中的消息框,它显示连接的字符串文本。我希望文本左对齐,但事实并非如此。我试着ljust()和{:<14}等,但它仍然没有对齐。
看起来像这样:
代码片段如下,
for todo_item in resp.json()['SectorList']:
sector_id +='Sector Id: {:<14}'.format(todo_item['SectorId']) + '\n'
sector_name += 'Sector Name: {:<40}'.format(todo_item['SectorName']) + '\n'
Run Code Online (Sandbox Code Playgroud)
在循环之后,我将这些文本添加到我的消息框中。
label_id = tkinter.Label(f, anchor = tkinter.W, text = sector_id)
label_name= tkinter.Label(f,anchor = tkinter.W, text = sector_name)
label_id.grid(row= 2, column = 1, sticky = tkinter.W)
label_name.grid(row= 2, column = 2, sticky = tkinter.W)
Run Code Online (Sandbox Code Playgroud)
扇区 ID 部分很好,但扇区名称未左对齐。任何的想法?
问题不在于您的代码,而在于字体。您使用的是非单位长度、非等宽字体,其中字符不占据相等的空间。要解决此问题,请切换到等宽字体,例如 Consolas。
import tkFont
my_font = tkFont.Font(family='Consolas', size=15, weight='bold')
label_id = tkinter.Label(f, anchor=tkinter.W, text=sector_id, font=('Consolas', 15))
label_name = tkinter.Label(f, anchor=tkinter.W, text=sector_name, font=('Consolas', 15))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3616 次 |
| 最近记录: |