tkinter 标签中的漂亮打印数据 [英] Pretty print data in tkinter Label

查看:38
本文介绍了tkinter 标签中的漂亮打印数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例数据

data=[(1,'JohnCena','Peter',24,74),(2,'詹姆斯','彼得',24,70),(3,'塞纳','彼得',14,64),(14,'约翰','火星',34,174)]

我想在 tkinter 输出窗口上以漂亮的表格方式在 python gui 上打印它.我正在使用

解决方案

问题:tabulate 输出,显示在 tk.Label 中,不会扭曲数据.

<小时>

正如评论中所指出的,这可以使用等宽字体来完成.
您必须使用以下 Label 选项,

justify=tk.LEFT锚='nw'

对齐表格left,并将其粘贴到左上角位置.

<小时>

参考:

  • 将 tkinter 导入为 tk从表格导入表格data = [('id', 'first name', 'last name', 'age', 'marks'),(1, 'JohnCena', 'Peter', 24, 74),(2, '詹姆斯', '彼得', 24, 70),(3, 'Cena', 'Peter', 14, 64),(14, '约翰', '火星', 34, 174)]类 TabulateLabel(tk.Label):def __init__(self, parent, data, **kwargs):super().__init__(parent,font=('Consolas', 10),justify=tk.LEFT, anchor='nw', **kwargs)text = tabulate(data, headers='firstrow', tablefmt='github', showindex=False)自我配置(文本=文本)类应用程序(tk.Tk):def __init__(self):super().__init__()TabulateLabel(self, data=data, bg='white').grid(sticky='ew')如果 __name__ == "__main__":应用程序().主循环()

    I have following sample data

    data=[(1,'JohnCena','Peter',24,74),
          (2,'James','Peter',24,70),
          (3,'Cena','Peter',14,64),
          (14,'John','Mars',34,174)]
    

    I want to print it on python gui in a beutiful tabular way on tkinter output window. I am using tabulate package to print. Here is my function

    def display_date():
        disp=pd.DataFrame(data,columns=['id','first name','last name','age','marks'])
        newwin = Toplevel(right_frame)
        newwin.geometry('500x400')
        Label_data=Label(newwin,text=tabulate(disp, headers='keys',tablefmt='github',showindex=False))
        Label_data.place(x=20,y=50)
    

    You can see the output is not symmetric. I want a beautiful symmetric tabular output. How can I do that

    Here is the output

    解决方案

    Question: tabulate output, displayed in a tk.Label, without to distort the data.


    As pointed out in the comments this can be done using a monospaced font.
    You have to use the following Label options,

    justify=tk.LEFT
    anchor='nw'
    

    to justify the table left, and stick it to top left position.


    Reference:


    import tkinter as tk
    from tabulate import tabulate
    
    data = [('id', 'first name', 'last name', 'age', 'marks'),
            (1, 'JohnCena', 'Peter', 24, 74),
            (2, 'James', 'Peter', 24, 70),
            (3, 'Cena', 'Peter', 14, 64),
            (14, 'John', 'Mars', 34, 174)
            ]
    
    
    class TabulateLabel(tk.Label):
        def __init__(self, parent, data, **kwargs):
            super().__init__(parent, 
                             font=('Consolas', 10), 
                             justify=tk.LEFT, anchor='nw', **kwargs)
    
            text = tabulate(data, headers='firstrow', tablefmt='github', showindex=False)
            self.configure(text=text)
    
    
    class App(tk.Tk):
        def __init__(self):
            super().__init__()
            TabulateLabel(self, data=data, bg='white').grid(sticky='ew')
    
    if __name__ == "__main__":
        App().mainloop()
    

    这篇关于tkinter 标签中的漂亮打印数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆