0

I am working to create a GUI in Tkinter and am trying to display a Pandas Dataframe in the main root window when the specific button is pressed.

I currently have a 'Calculate' button that when pressed it displays the dataframe but it opens in a new top level window.

Here is my current code:


def calculate_button(x, y, n, m):
    global results
    results = funct(x, y, n, m)
    print(results)
    
    results_output = tk.Toplevel()
    results_output.title('TKinter App')
    results_window = Table(results_output, dataframe=results, showtoolbar=False, showstatusbar=False)
    results_window.show()

How do i made the adjustment to have this be placed on the root TKinter window using .grid so it aligns with everything else on the root. Is this possible?

Tristan
  • 31
  • 4

1 Answers1

-2

Yes, it is possible to display the Pandas DataFrame on the main root window using grid so that it aligns with other widgets. To achieve this, you can follow these steps:

  1. Create a new frame inside the main root window where you want to display the DataFrame.
  2. Instead of using Toplevel, use the new frame to display the DataFrame.
  3. Use grid layout manager to place the new frame in the desired position within the root window.

Here's the modified code:

import tkinter as tk
from pandastable import Table  # Assuming you are using pandastable to display DataFrames

def calculate_button(x, y, n, m):
    global results
    results = funct(x, y, n, m)
    print(results)
    
    # Create a new frame inside the root window to display the DataFrame
    results_frame = tk.Frame(root)  # Replace 'root' with the actual name of your main root window
    results_frame.grid(row=row_number, column=column_number, padx=10, pady=10)  # Adjust row and column number as per your requirement
    
    # Create the PandasTable inside the new frame
    results_window = Table(results_frame, dataframe=results, showtoolbar=False, showstatusbar=False)
    results_window.show()

# Create your Tkinter main root window
root = tk.Tk()

# Add other widgets and layouts using grid or pack as necessary

# Example usage of the calculate_button function
calculate_button(x, y, n, m)

# Start the main event loop
root.mainloop()

Make sure to replace 'root' with the actual name of your main root window, and adjust the row and column numbers in results_frame.grid according to where you want the DataFrame to be displayed.

With these changes, the DataFrame will be displayed inside the new frame within the main root window using the grid layout manager.

  • Welcome to Stack Overflow, Saunak Ghosh! Both of your answers so far here appear likely to be entirely or partially written by AI (e.g., ChatGPT). Please be aware that [posting AI-generated content is not allowed here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. We do hope you'll stick around and continue to be a valuable part of our community by posting *your own* quality content. Thanks! – NotTheDr01ds Jul 25 '23 at 20:03
  • **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. – NotTheDr01ds Jul 25 '23 at 20:03