I have a Table i am generating using a List (Baseline_Data).
I can get this to print to the terminal fine using:
for row in Baseline_Data:
# Display List as Table
print('\t'.join(row))
What i cant get to work is for this to display in my Tkinter GUI. I found the best way to achieve this would be to generate a label and configure the text.
I have tried:
Import_Lab.config(text=("\t".join(row)))
Which only displays the last row of my List
Import_Lab.config(text=('\n'.join(row)))
Which does the same but displays it as a column
Import_Lab.config(text=(Baseline_Data))
Which displays the full List but as one long continuous line.
How do i display this List as a Multi-line Label ?
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()
You need to generate the complete string, with all the lines (rows of the table) and then pass it to the text
argument, for example:
Import_Lab.config(text="\n".join("\t".join(row) for row in Baseline_Data))
For the table to display correctly, 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()
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()