0

Once again I'm here in this awesome community asking for help. I found some similar questions, but not exactly like that.

I wrote a program in Python with the porpouse to be a didatic tool (as the commercial softwares are so expensive).

My code are already finished and now I'm trying to put it in a simple GUI to my students. (I'm using tkinter, by the way)

The code beneath is a parte of it where I'm calling the tk application for importing data through a import wizard and it stores the data as a pandas dataframe.

import tkinter as tk
from tkinter import filedialog
import pandas as pd
import numpy as np

#import data
ascii_well=filedialog.askopenfilename()
df_test=pd.read_csv(ascii_well, sep="\s+", index_col=0, na_values=-999.25)

print(df_teste)

Out[5]: 
       a  b   c
 IDX                  
 0      2.1   2.2   30
 1      2.2   2.3  150

What I want to put in my GUI is a way to display this DataFrame (if it was easyer to implement, could be an array instead). But i don't want to just diplay the data to the user. I want to implement is a way that the end-user could not only see the data, but also change the values of the dataframe.

For a simple exemple

Let's say the user want to change de value 2.1 (column "a", IDX=0) to 3.

I'm writing my code on spyder, Python 3.

Thanks, guys!!

GabrielBR
  • 25
  • 1
  • 6
  • Check out my answer on a similar question [here](https://stackoverflow.com/questions/45264826/printing-the-output-of-a-script-to-a-window-in-python/45270752#45270752) – Mike - SMT Jul 24 '17 at 17:53
  • Possible duplicate of [Printing the output of a script to a window in python](https://stackoverflow.com/questions/45264826/printing-the-output-of-a-script-to-a-window-in-python) – Mike - SMT Jul 24 '17 at 17:53
  • We also need more code to test with. How do you currently manipulate the data frame. This can be converted to a function that uses a value submitted to an entry widget the user can type in. – Mike - SMT Jul 24 '17 at 17:56
  • In this specific case I just use the DataFrame becouse I used the pandas for importing the data to project. And the stored data has different subdata, but genetically related (each column is a parameter of a specific group) and I can call each data by his "proper name". Easy for me to work – GabrielBR Jul 25 '17 at 11:51

1 Answers1

0

Here is a simple example of how you can manipulate a variable from a tkinter GUI using the Entry widget for user data.

This example is very simple but should provide a good illustration on how you can get data from the user and save it to a variable and then display that data.

You can modify the methods to work with you database and go from there.

import tkinter as tk

class tester(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)       

        self.parent = parent
        self.my_var = ""
        self.ent0 = tk.Entry(self.parent)
        self.ent0.pack()

        self.btn1 = tk.Button(self.parent, text="Save Values", command = self.save_values)
        self.btn1.pack()
        self.btn2 = tk.Button(self.parent, text="Load Values", command = self.load_values)
        self.btn2.pack()

    def save_values(self):
        print(self.ent0.get())
        self.my_var = self.ent0.get()
        self.ent0.delete(0, tk.END)

    def load_values(self):

        self.ent0.delete(0, tk.END)
        self.ent0.insert(tk.END, self.my_var)


if __name__ == "__main__":
    root = tk.Tk()
    app = tester(root)
    root.mainloop()

Per your comment I have made a non class version of this code below:

import tkinter as tk


root = tk.Tk()

my_var = ""
ent0 = tk.Entry(root)
ent0.pack()

def save_values():
    global my_var, ent0
    my_var = ent0.get()
    ent0.delete(0, tk.END)

def load_values():
    global my_var, ent0
    ent0.delete(0, tk.END)
    ent0.insert(tk.END, my_var)

btn1 = tk.Button(root, text="Save Values", command = save_values)
btn1.pack()
btn2 = tk.Button(root, text="Load Values", command = load_values)
btn2.pack()

root.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • Thank you very much! I don't have much experience with classes, i don't know how to work with that, but I run your code, and i think i can adapt it to my objetctives. Thanks! – GabrielBR Jul 25 '17 at 11:52
  • @GabrielBR I have added a non class version of my example so you have that to work with as well. – Mike - SMT Jul 25 '17 at 13:01
  • thank you so much. Recently I posted another topic. If you could check it, i'd appretiate it! https://stackoverflow.com/questions/45300287/how-to-make-expressions-in-tkinter-python?noredirect=1#comment77564169_45300287 – GabrielBR Jul 25 '17 at 14:32