3 回答
TA贡献1810条经验 获得超5个赞
直接的方法是使用。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()
它显示:
TA贡献1809条经验 获得超8个赞
您可以使用string formatting
和固定/等宽字体:
listbox = Listbox(students_Box, width=90, bg='azure', font='Courier 11') # use a fixed/monospaced font
...
for rec in allStudents:
listbox.insert(END, f"{rec['Reg']:15}{rec['Name']:30}{rec['Email']}")
如果您不喜欢硬编码宽度,则可以计算每列的最大宽度:
widths = {key:0 for key in allStudents[0]}
for rec in allStudents:
for key in rec:
widths[key] = max(widths[key], len(rec[key]))
for rec in allStudents:
listbox.insert(END, f"{rec['Reg']:{widths['Reg']}} {rec['Name']:{widths['Name']}} {rec['Email']}")
TA贡献1936条经验 获得超6个赞
您可以尝试以下代码。您必须使用等宽字体才能使脚本正常工作
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 = ('Consolas', 10, ''))#'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
MaxRegLength = len(max(allStudents, key=lambda s: len(s['Reg']))['Reg'])
MaxNameLength = len(max(allStudents, key=lambda s: len(s['Name']))['Name'])
print(MaxRegLength, MaxNameLength)
# MaxEmailLength = max(allStudents, key=lambda s: len(s['name']))
for i in range(len(allStudents)):
listbox.insert(END,
'%s %s %s'%(allStudents[i]['Reg'].ljust(MaxRegLength), allStudents[i]['Name'].ljust(MaxNameLength), allStudents[i]['Email']))
# f"{allStudents[i]['Reg'].ljust(MaxRegLength)} {allStudents[i]['Name'].ljust(MaxNameLength)} {allStudents[i]['Email']}")
listbox.configure(yscrollcommand = scrollbar.set)
scrollbar.configure(command = listbox.yview)
mainloop()
添加回答
举报