Home > Mobile >  Importing a dataset with button with tkinter
Importing a dataset with button with tkinter

Time:10-02

I am making a program where a user will choose a dataset from a dropdown box, and will subsequently then load the data in for analysis later on in the program. My current problem is that the data is not being loaded in, and I am not sure why

from tkinter import *

datasets = ["dataset 1", "dataset 2", "dataset 3", "dataset 4"]
master = Tk()

variable = StringVar(master)
variable.set(datasets[0])

data_options = OptionMenu(master, variable, *datasets)
data_options.pack()

from sklearn import datasets
import pandas as pd

def dat_import():
    if variable.get() == "dataset 1":
        data = pd.DataFrame(datasets.load_iris().data)
        return data
    if variable.get() == "dataset 2":
        data = pd.DataFrame(datasets.load_diabetes().data)
        return data
    if variable.get() == "dataset 3":
        data = pd.DataFrame(datasets.load_digits().data)
        return data
    if variable.get() == "dataset 4":
        data = pd.DataFrame(datasets.load_boston().data)
        return data

button = Button(master, text = "Import Dataset", command = dat_import)
button.pack()

mainloop()

Simply, all I need is a user to select choice from dropdown box, press import dataset and that dataset is saved as a dataframe for access later in the program.

Any help would be greatly appreciated, because I cannot find anywhere that will explain how to do this!

CodePudding user response:

You should not return anything from a button callback, as it is not accessible. Instead globalize it so you can use it in a later function or anywhere else:

def dat_import():
    global data

    if variable.get() == datasets[0]:
        data = pd.DataFrame(datasets.load_iris().data)
    elif variable.get() == datasets[1]:
        data = pd.DataFrame(datasets.load_diabetes().data)
    elif variable.get() == datasets[2]:
        data = pd.DataFrame(datasets.load_digits().data)
    else:
        data = pd.DataFrame(datasets.load_boston().data)

A better way to reduce the code might be to use dictionary dispatching here, so something like:

datasets = ["dataset 1", "dataset 2", "dataset 3", "dataset 4"]
dis = {datasets[0] : pd.DataFrame(datasets.load_iris().data,
       datasets[1] : pd.DataFrame(datasets.load_diabetes().data,
       datasets[2] : pd.DataFrame(datasets.load_digits().data,
       datasets[3] : pd.DataFrame(datasets.load_boston().data,
       }

def dat_import():
    global data

    data = dis[variable.get()]
  • Related