python - How to align text in a tkinter Listbox
问题描述
I want to align text within a tkinter listbox. I have a list of dictionaries with reg. no., name, and email as keys in each dictionary. I have looped over the list and want the three attributes to be aligned in such a way, that there is equal distance between these words. So every word in every row has to start at the same position than the word in the row before / afterwards. But due to different name lengths, the alignment is disturbed. In the code above, for simplicity i have just created 4 dictionaries for 4 students, but actually there are more than 50 students that I have to manage. Also is there another simple way to display this data other than the list box, with the same condition stated above?
from tkinter import *
root = Tk()
students_Box = LabelFrame(root,
text = 'Registered Students', bd = 4,
relief = GROOVE, labelanchor = 'n',
font = 'Arial 10 bold', fg = 'navy blue',
width = 850, height = 180)
students_Box.grid_propagate(0)
students_Box.pack(pady = 15)
scrollbar = Scrollbar(students_Box)
scrollbar.pack(side = RIGHT, fill = Y)
listbox = Listbox(students_Box, width = 90, bg = 'azure', font = 'TkDefaultFont 11')
listbox.pack(padx = 5, pady = 10)
allStudents = [{'Reg': '2018-MC-01', 'Name': 'Usman Khan', 'Email': '2018mc01@student.uet.edu.pk'},
{'Reg': '2018-MC-02', 'Name': 'Muhammad Ehtisham Zafar', 'Email': '2018mc02@student.uet.edu.pk'},
{'Reg': '2018-MC-03', 'Name': 'Ali Khan', 'Email': '2018mc03@student.uet.edu.pk'},
{'Reg': '2018-MC-04', 'Name': 'Abdullah Akram', 'Email': '2018mc04@student.uet.edu.pk'}] # A List of Dictionaries
for i in range(len(allStudents)):
listbox.insert(END, f"{allStudents[i]['Reg']} {allStudents[i]['Name']}{' '*20}{allStudents[i]['Email']}")
listbox.configure(yscrollcommand = scrollbar.set)
scrollbar.configure(command = listbox.yview)
mainloop()
解决方案
直接的方法是使用ttk.Treeview
。根据您的情况的解决方案(用于ljust
填充它们):
from tkinter import *
root = Tk()
students_Box = LabelFrame(root,
text='Registered Students', bd=4,
relief=GROOVE, labelanchor='n',
font='Arial 10 bold', fg='navy blue',
width=850, height=180)
students_Box.grid_propagate(0)
students_Box.pack(pady=15)
scrollbar = Scrollbar(students_Box)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(students_Box, width=90, bg='azure', font='TkDefaultFont 11')
listbox.pack(padx=5, pady=10)
allStudents = [{'Reg': '2018-MC-01', 'Name': 'Usman Khan', 'Email': '2018mc01@student.uet.edu.pk'},
{'Reg': '2018-MC-02', 'Name': 'Muhammad Ehtisham Zafar', 'Email': '2018mc02@student.uet.edu.pk'},
{'Reg': '2018-MC-03', 'Name': 'Ali Khan', 'Email': '2018mc03@student.uet.edu.pk'},
{'Reg': '2018-MC-04', 'Name': 'Abdullah Akram',
'Email': '2018mc04@student.uet.edu.pk'}] # A List of Dictionaries
maxspace = len(max(allStudents, key=lambda x:len(x['Name']))['Name']) + 4 # add 4 spaces.
for i in range(len(allStudents)):
listbox.insert(END, f"{allStudents[i]['Reg']} {allStudents[i]['Name'].ljust(maxspace)}{allStudents[i]['Email']}")
listbox.configure(yscrollcommand=scrollbar.set)
scrollbar.configure(command=listbox.yview)
mainloop()
推荐阅读
- python-3.x - 我的代码在 Wikipedia 中找不到表格
- hibernate - Hazelcast 4.0.2 和 hazelcast hibernate4 3.8.4 之间的接口
- javascript - JavaScript 将对象映射到数组
- deep-learning - 梯度裁剪会降低 RNN 的有效性吗
- javascript - 您如何将 google maps 高程服务用作 jsx 标签?
- python - 如何理解 apply 函数来计算 pandas 中使用的加权平均值?
- c++ - 如何释放 new[] 分配的内存?
- javascript - 如何通过json_encode重定向php页面
- mysql - 需要帮助连接 SQL
- c# - 使用 c# 与 java/c++ 比较的 grpc 的性能基准数