How can i create a button witk Tkinter which prints a specific DataFrame section?

Viewed 49

I need a to create a button which displays a specific section of a filtered DataFrame

First, the DataFrame is built with the following lines of code:

def filter():
    Data= df.loc[df["A"] == "Native"]
    Data_2 = Data.loc[Data["B"] == Market] 
    Data_3 = Data_2.loc[Data_2["C"] == Class]
    Data_4 = Data_3.loc[Data_3["D"] == Date]
    print(Data_4 )

Then the GUI is the following

from tkinter import *
root = Tk()
root.geometry("600x600")
root.title("GUI NTV-TRN")

register_button = Button(root,text= "Enter", command = filter())
register_button.pack()

root.mainloop()

The problem is that when i launch the GUI, the printed output remains into my IDE and it is not displayed into the GUI itself. How can i fix this?

Thanks

1 Answers

Where will you like to display this information, on the same window or on a pop-up window?

At the moment is only displaying at the IDE because the print command on the filter function will only print into the CLI.

you will need to create a label for example with the return text of the filter function or you can use the .configure method of the label you created.

Let's say you wish to display the framework on the same GUI windows, you can modify the GUI logic by adding after the button.pack line the following:

data_frame_label = Label(root, text="")
data_frame_label.pack()

then on the frame function you add at the end the following line

data_frame_label.configure(text=Data_4)

Data_4 or the name of the variable that has the text you wish to display

so you code should look something like this:

def filter():
    Data= df.loc[df["A"] == "Native"]
    Data_2 = Data.loc[Data["B"] == Market] 
    Data_3 = Data_2.loc[Data_2["C"] == Class]
    Data_4 = Data_3.loc[Data_3["D"] == Date]
    data_frame_label.configure(text=Data_4)
    print(Italo_4)

Then you may delete the line print(Italo_4) if not needed any more

Also, when calling the filter function from the button, do not use the parenthesis"()".

from tkinter import *
root = Tk()
root.geometry("600x600")
root.title("GUI NTV-TRN")

register_button = Button(root,text= "Enter", command = filter)
register_button.pack()

data_frame_label = Label(root, text="")
    data_frame_label.pack()

root.mainloop()
Related