-2

I'm currently trying to print a table of data gathered from a database onto a GUI in tkinter, at the moment I'm using tabulate which runs fine in console, but as soon as I try to set the table as text as print as a label the table skews and the values are not aligned. I am still new to tkinter and so my code is by far not the most efficient or effective, any ideas on why this is happening or a way around?

        order = (key_1,name_1,price_1,quantity_1,total_1)
        table.append(order)
        count +=1
    print_table = (tabulate(table,headers=headers,tablefmt= "rst"))
    c.fetchall()
    conn.commit()
    conn.close()
    label_table = tk.Label(self,text=print_table)
    label_table.pack()

Expected Result: !file:///var/folders/59/89rdtwpd4vnf6pv7pbd3l60m0000gn/T/com.apple.Safari/WebKitDropDestination-rwh55dGn/Screen%20Shot%202019-02-03%20at%2011.49.29%20PM.png

Actual Result:

!file:///var/folders/59/89rdtwpd4vnf6pv7pbd3l60m0000gn/T/com.apple.Safari/WebKitDropDestination-tZziKT9D/Screen%20Shot%202019-02-03%20at%2011.47.15%20PM.png

https://i.stack.imgur.com/bgG4C.png

  • You could give the `Treeview` widget a try: https://tkdocs.com/tutorial/tree.html and http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Treeview.html –  Feb 03 '19 at 19:57

2 Answers2

0

I would highly recommend creating a grid of tk.Entry widgets where you place your headers and values in discrete entries. If you recorded the tk.Entry widget objects in a list you would be able to call on them individually outside of the loop if you preferred that. You could substitute the tk.Label widget in the place of tk.Entry with other formatting changes if you were partial to the Label widget.

tableWindow=tk.Toplevel()
tableHeaderList=["Header1","Header2","Header3","Header4"]

for i in range(height): #Rows
    for j in range(width): #Columns
        b = tk.Entry(tableWindow, text="")
        b.grid(row=i, column=j)
        if i == 0:
            b.insert(0,tableHeaderList[j])
whisperquiet
  • 27
  • 2
  • 8
0

I suggest you to use a monospaced font. I face the same problem and use Cascadia Mono font and it worked. use the parameter font=("Cascadia Mono", 10) in the label. Hope it helps.