我有一个使用列表(Baseline_Data)生成的表.

我可以使用以下命令将此代码打印到终端:

for row in Baseline_Data:
    # Display List as Table
    print('\t'.join(row))

我不能让它显示在我的Tkinter图形用户界面上.我发现实现这一点的最好方法是生成一个标签并配置文本.

我试过了:

Import_Lab.config(text=("\t".join(row)))

它只显示我的列表的最后一行

Import_Lab.config(text=('\n'.join(row)))

它执行相同的操作,但将其显示为一列

Import_Lab.config(text=(Baseline_Data))

它显示完整的列表,但显示为一条长而连续的线.

如何将此列表显示为多行标签?

import tkinter as tk
from tkinter import ttk
from tkinter import *

root = Tk()
note = ttk.Notebook(root)

def Import_Button1_Press():

    Baseline_Data = [
    ['     IM      |', 'IM.Ser', 'Maj', 'Min', 'IIB.Ser', 'Maj', 'Min', 'ManfDate'],
    ['---------------------------------------------------------------------------'],
    ['1 - IM1 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
    ['1 - IM2 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
    ['1 - IM3 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
    ['1 - IM4 |', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA'],
    ['1 - IM5 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
    ['1 - IM6 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
    ['2 - IM1 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
    ['2 - IM2 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
    ['2 - IM3 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
    ['2 - IM4 |', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA'],
    ['2 - IM5 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
    ['2 - IM6 |',    "12345", "22", "33", "12345", "55", "66", "123456"]
    ]

    for row in Baseline_Data:
        # Display List as Table
        print('\t'.join(row))

Tab1 = ttk.Frame(note)

canvas1 = Canvas(Tab1, width=550, height=350)
canvas1.pack()

Tab2 = ttk.Frame(note)

canvas2 = Canvas(Tab2, width=550, height=350)
canvas2.pack()

Import_Button1 = tk.Button(Tab2, text = 'Import XML [Baseline]', width=25, height=2, command=Import_Button1_Press)
Import_Button_Window = canvas2.create_window(25, 40, anchor = 'w', window = Import_Button1)

Import_Lab = Label(Tab2, anchor=W)
Import_Lab_Window = canvas2.create_window(275, 175, anchor = 'center', window = Import_Lab)

note.add(Tab1, text = " Main ")
note.add(Tab2, text = " Baseline Data ")

note.pack()
root.mainloop()

推荐答案

您需要生成包含所有行(表的行)的完整字符串,然后将其传递给text参数,例如:

Import_Lab.config(text="\n".join("\t".join(row) for row in Baseline_Data))

要使表格正确显示,请将其设置为use a monospace font for that label.

from tabulate import tabulate
import tkinter as tk
from tkinter import ttk


def import_button1_press():

    baseline_data = [
        ['     IM      |', 'IM.Ser', 'Maj', 'Min', 'IIB.Ser', 'Maj', 'Min', 'ManfDate'],
        ['---------------------------------------------------------------------------'],
        ['1 - IM1 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['1 - IM2 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['1 - IM3 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['1 - IM4 |',       'NA', 'NA', 'NA',    'NA', 'NA', 'NA',     'NA'],
        ['1 - IM5 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['1 - IM6 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['2 - IM1 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['2 - IM2 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['2 - IM3 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['2 - IM4 |',       'NA', 'NA', 'NA',    'NA', 'NA', 'NA',     'NA'],
        ['2 - IM5 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['2 - IM6 |',    "12345", "22", "33", "12345", "55", "66", "123456"]
    ]
    
    table = "\n".join("\t".join(row) for row in baseline_data)
    import_lab.config(text=table)


root = tk.Tk()
note = ttk.Notebook(root)

tab1 = ttk.Frame(note)

canvas1 = tk.Canvas(tab1, width=850, height=650)
canvas1.pack()

tab2 = ttk.Frame(note)

canvas2 = tk.Canvas(tab2, width=850, height=650)
canvas2.pack()

import_button1 = tk.Button(
    tab2, text='Import XML [Baseline]',
    width=25, height=2,
    command=import_button1_press
    )
import_button_window = canvas2.create_window(
    25, 40, anchor='w', window=import_button1
    )

import_lab = tk.Label(tab2, font=('Consolas', 10), justify=tk.LEFT, anchor='nw')
import_lab_window = canvas2.create_window(
    425, 350, anchor='center', window=import_lab
    )

note.add(tab1, text=" Main ")
note.add(tab2, text=" Baseline Data ")

note.pack()
root.mainloop()

enter image description here Another option to generate plain text tables easily is to use the tabulate package:

from tabulate import tabulate
import tkinter as tk
from tkinter import ttk


def import_button1_press():

    baseline_data = [
        ['     IM      |', 'IM.Ser', 'Maj', 'Min', 'IIB.Ser', 'Maj', 'Min', 'ManfDate'],
        ['---------------------------------------------------------------------------'],
        ['1 - IM1 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['1 - IM2 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['1 - IM3 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['1 - IM4 |',       'NA', 'NA', 'NA',    'NA', 'NA', 'NA',     'NA'],
        ['1 - IM5 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['1 - IM6 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['2 - IM1 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['2 - IM2 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['2 - IM3 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['2 - IM4 |',       'NA', 'NA', 'NA',    'NA', 'NA', 'NA',     'NA'],
        ['2 - IM5 |',    "12345", "22", "33", "12345", "55", "66", "123456"],
        ['2 - IM6 |',    "12345", "22", "33", "12345", "55", "66", "123456"]
    ]

    table = tabulate(
        baseline_data[2:], headers=baseline_data[0], tablefmt="fancy_grid"
        )
    import_lab.config(text=table)


root = tk.Tk()
note = ttk.Notebook(root)

tab1 = ttk.Frame(note)

canvas1 = tk.Canvas(tab1, width=850, height=650)
canvas1.pack()

tab2 = ttk.Frame(note)

canvas2 = tk.Canvas(tab2, width=850, height=650)
canvas2.pack()

import_button1 = tk.Button(
    tab2, text='Import XML [Baseline]',
    width=25, height=2,
    command=import_button1_press
    )
import_button_window = canvas2.create_window(
    25, 40, anchor='w', window=import_button1
    )

import_lab = tk.Label(tab2, font=('Consolas', 8), justify=tk.LEFT, anchor='nw')
import_lab_window = canvas2.create_window(
    425, 325, anchor='center', window=import_lab
    )

note.add(tab1, text=" Main ")
note.add(tab2, text=" Baseline Data ")

note.pack()
root.mainloop()

enter image description here

Python相关问答推荐

如何使用Jinja语法在HTML中重定向期间传递变量?

如何在Python中将returns.context. DeliverresContext与Deliverc函数一起使用?

删除所有列值,但判断是否存在任何二元组

如何在Django基于类的视图中有效地使用UTE和RST HTIP方法?

Python—从np.array中 Select 复杂的列子集

下三角形掩码与seaborn clustermap bug

numpy.unique如何消除重复列?

如何检测鼠标/键盘的空闲时间,而不是其他输入设备?

Python避免mypy在相互引用中从另一个类重定义类时失败

OpenGL仅渲染第二个三角形,第一个三角形不可见

Gekko中基于时间的间隔约束

polars:有效的方法来应用函数过滤列的字符串

如何在Python Pandas中填充外部连接后的列中填充DDL值

为什么t sns.barplot图例不显示所有值?'

如何在验证文本列表时使正则表达式无序?

为什么我只用exec()函数运行了一次文件,而Python却运行了两次?

以极轴表示的行数表达式?

如何在不不断遇到ChromeDriver版本错误的情况下使用Selify?

迭代工具组合不会输出大于3的序列

如何通过函数的强式路径动态导入函数?