How to use user input that was taken from a simple pyton tkinter gui

Issue

This Content is from Stack Overflow. Question asked by Julian

I would like to take input from a user, then create a variable that stores this input and use it in the next part of code. For example I would like to take a filename from user, then create a dataframe and then do some dataframe manipulation after the gui has been closed.

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

def show_entry_fields():
    print("First Name: %snLast Name: %s" % (e1.get(), e2.get()))

def UploadAction(event=None):
    filename = filedialog.askopenfilename()
    print('Selected:', filename)

def UploadAction2(event=None):
    filename2 = filedialog.askopenfilename()
    print('Selected:', filename2)

master = tk.Tk()
master.title('App')
master.geometry('300x120')
master['bg'] = '#ffbf00'
tk.Label(master,
         text="Something").grid(row=0)
tk.Label(master,
         text="Something").grid(row=1)

e1 = tk.Entry(master)
e2 = tk.Entry(master)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)

tk.Button(master,
          text='Close',
          command=master.quit).grid(row=2,
                                    column=0,
                                    sticky=tk.W,
                                    pady=4)
tk.Button(master,
          text='Save', command=show_entry_fields).grid(row=2,
                                                       column=1,
                                                       sticky=tk.W,
                                                       pady=4)

tk.Button(master, text='Open', command=UploadAction).grid(row=3,column=0,sticky=tk.W,pady=4)
tk.Button(master, text='Open', command=UploadAction2).grid(row=3,column=1,sticky=tk.W,pady=4)
tk.mainloop()



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?