Apprehensive_Cow
Apprehensive_Cow

Reputation: 1

Display a dataframe in Tkinter in a new window

I'm looking into making a button that displays a dataframe in a new window in my Tkinter app. Did some googling but couldn't find anything. Any ideas? Much appreciated!!


class MyApp():
    def __init__(self, master):
        self.master = master
        master.title("My App")
        self.button1 = Button(master, text="Display df", command=self.display_df_in_new_window)
        self.button1.place(x=60, y=100,height = 44, width = 127)

    def get_data(self):
      ##code to get data
        return df

    def display_df_in_new_window(self):
       ## code

root = Tk()
my_gui = MyApp(root)
root.geometry("600x450")
root.mainloop()

Upvotes: 0

Views: 3201

Answers (1)

Zain Ul Abidin
Zain Ul Abidin

Reputation: 2700

I modified your code a bit so that it is now opening a new window to show data frame of pandas, for test purpose i generated dummy test data, you can replace that part with your own data frame to show.

from tkinter import *
from pandastable import Table, TableModel

class MyApp():
    def __init__(self, master):
        self.master = master
        master.title("My App")
        self.button1 = Button(master, text="Display df", command=self.display_df_in_new_window)
        self.button1.place(x=60, y=100,height = 44, width = 127)

    def get_data(self):
      ##code to get data
      #generating sample data for test purpose replace with your own df
        df = TableModel.getSampleData()
        return df

    def display_df_in_new_window(self):
       frame = Toplevel(self.master) #this is the new window
       self.table = Table(frame, dataframe=self.get_data(), showtoolbar=True, showstatusbar=True)
       self.table.show()

root = Tk()
my_gui = MyApp(root)
root.geometry("600x450")
root.mainloop()

Output will be something like below

Output screenshot on Ubuntu 20.04

Upvotes: 2

Related Questions