首页 > 解决方案 > 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()

标签: pythontkinterlistboxalignment

解决方案


直接的方法是使用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()

它显示: 在此处输入图像描述


推荐阅读