将列表格式化为列

时间:2019-02-03 14:57:50

标签: python-3.x tkinter formatting

我正在尝试像这样拆分列:

ID    Username    Password    Clearance    Class
1     foo         bar         Admin        True
etc   etc         etc         etc          etc

使用“ {0:> 8}”。format(* i)方法

可以打印到控制台,并且格式很好,但是当我将文本传递到用于显示搜索结果的弹出窗口时,格式更像这样:

ID    Username    Password    Clearance    Class
 1     foo    bar      Admin   False

代码如下:

from tkinter.messagebox import showinfo

User_List = [["foo","bar","admin","true"],["fa","lo","user","false"]]

def Main():
    Display_Text_List = [["Index","Username","Password","Clearance","Class"]]
    for Count,Sub_List in enumerate(User_List):
        ID = str(Count+1)
        Username = Sub_List[0]
        Password = Sub_List[1]
        Clearance = Sub_List[2]
        Class = Sub_List[3]
        Display_Text_List.append([ID,Username,Password,Clearance,Class])
    Display_Text = Column_Format(Display_Text_List)
    print("Display_Text :\n")
    print(Display_Text)
    Popup_Show_Info(Display_Text)

def Popup_Show_Info(text):
    showinfo("Window", text)

def Column_Format(List):
    Text = ""
    print("LIST:",List)
    for i in List:
        print("i:",i)
        Text_Extension = "{0:>8} {1:>12} {2:>12} {3:>12} {4:>10}".format(*i)
        Text += Text_Extension+"\n"
    return Text

if __name__ == "__main__":
    Main()

1 个答案:

答案 0 :(得分:0)

您需要向根目录添加一些选项。为了使您的示例生效,请将其添加到顶部:

import tkinter
r = tkinter.Tk()
r.option_add('*Dialog.msg.font', 'Courier') # set the font for dialog boxes
r.option_add('*Dialog.msg.wrapLength', '800') # set the max width of dialog boxes
r.withdraw() # Hide the main window

在实际程序中,应使用根窗口而不是“ r”。