Tkinter标签中的漂亮打印数据
我有以下示例数据
data=[(1,'JohnCena','Peter',24,74),
(2,'James','Peter',24,70),
(3,'Cena','Peter',14,64),
(14,'John','Mars',34,174)]
我想在tkinter输出窗口上以漂亮的表格方式在python gui上打印它.我正在使用制表程序包进行打印. 这是我的功能
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
这是输出
问题:
tabulate
输出,显示在tk.Label
中,不会使数据失真.
Question:
tabulate
output, displayed in atk.Label
, without to distort the data.
正如注释中指出的,可以使用monospaced font
来完成.
您必须使用以下Label
选项
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'
证明表格left
合理,并将其粘贴在左上方.
to justify the table left
, and stick it to top left position.
参考:
- The Tkinter Label Widget
- tabulate
- Print tabular formated text into a tk.Text widget, not aligned as its supposed.
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()