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': '[email protected]'},
{'Reg': '2018-MC-02', 'Name': 'Muhammad Ehtisham Zafar', 'Email': '[email protected]'},
{'Reg': '2018-MC-03', 'Name': 'Ali Khan', 'Email': '[email protected]'},
{'Reg': '2018-MC-04', 'Name': 'Abdullah Akram',
'Email': '[email protected]'}] # 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條經驗 獲得超7個贊
您可以嘗試以下代碼。您必須使用等寬字體才能使腳本正常工作
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': '[email protected]'},
{'Reg': '2018-MC-02', 'Name': 'Muhammad Ehtisham Zafar', 'Email': '[email protected]'},
{'Reg': '2018-MC-03', 'Name': 'Ali Khan', 'Email': '[email protected]'},
{'Reg': '2018-MC-04', 'Name': 'Abdullah Akram', 'Email': '[email protected]'}] # 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()
添加回答
舉報