0

I am trying to display a pandas dataframe in a tkinter label in a popup window so that the user can easily see the data.

Here is the code:

from tkinter import Toplevel, Button, Tk
import tkinter as tk
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_colwidth', 50)
pd.set_option('display.width',200)

root = Tk()

root.geometry("550x600+500+200")
df = pd.DataFrame.from_dict({'title': {0: 'Sober', 1: 'Sober (8-Bit Tool Emulation) - 8-Bit Tool Emulation', 2: 'Sober (1993) [8-Bit Tool Emulation]', 3: 'Tool No. 2 (Sober)', 4: 'Sober (Made Famous by Tool)'}, 'artist': {0: 'Tool', 1: '8-Bit Arcade', 2: '8-Bit Arcade', 3: 'UltraRock', 4: 'Baby Lullaby Ensemble'}, 'endPos': {0: 38235, 1: 46546, 2: 56462, 3: 65970, 4: 74870}, 'match': {0: 100, 1: 29, 2: 37, 3: 57, 4: 35}, 'url': {0: 'https://www.musixmatch.com/lyrics/Tool-4/sober?utm_source=application&utm_campaign=api&utm_medium=musixmatch-community%3A1409608317702', 1: 'https://www.musixmatch.com/lyrics/8-Bit-Arcade/Sober-8-Bit-Tool-Emulation-8-Bit-Tool-Emulation?utm_source=application&utm_campaign=api&utm_medium=musixmatch-community%3A1409608317702', 2: 'https://www.musixmatch.com/lyrics/8-Bit-Arcade/Sober-1993-8-Bit-Tool-Emulation?utm_source=application&utm_campaign=api&utm_medium=musixmatch-community%3A1409608317702', 3: 'https://www.musixmatch.com/lyrics/UltraRock/Tool-No-2-Sober?utm_source=application&utm_campaign=api&utm_medium=musixmatch-community%3A1409608317702', 4: 'https://www.musixmatch.com/lyrics/Baby-Lullaby-Ensemble/Sober-Made-Famous-by-Tool?utm_source=application&utm_campaign=api&utm_medium=musixmatch-community%3A1409608317702'}}
)

def new_window() :
    win = Toplevel()
    win.geometry("%dx%d+%d+%d" % (1050, 270, root.winfo_x()  , root.winfo_y() + 600/4))
    tk.Label(win, text= df).grid(row=0, sticky=(tk.E))

Button(root, text = "open", command = new_window).pack()

root.mainloop()

but this is what the output looks like:

enter image description here

How do I make the values and headers left align? Or is there another way to display this nicer? I am not interested in using actual tables that look like excel, just want to make this more readable.

1
  • Use monospaced font and set justify="left". Or use Text widget. Commented Jan 2, 2024 at 0:57

1 Answer 1

2

You can simply use TkFixedFont to get a better result:

tk.Label(win, text=df, font='TkFixedFont').grid(row=0, sticky=(tk.E))

In addition, use df.to_string if you want more control (remove index, formatting numbers, etc)

text = df.to_string(index=False, max_colwidth=80)
tk.Label(win, text=text, font='TkFixedFont').grid(row=0, sticky=(tk.E))

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.