FJC_92
FJC_92

Reputation: 3

tkinter: AttributeError: 'Button' object has no attribute 'get' when trying to access file from filedialog.askopenfilename

So I have something like this:

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

window = tk.Tk()
window.geometry('350x240')

def open_file():
    temp_file = filedialog.askopenfilename(title="Open file", filetypes=[("Excel files", "*.csv")])
    temp_file = open(temp_file, "r")
    Proj_df = pd.read_csv(temp_file)

open_button = ttk.Button(text='Select File...', command=open_file)
open_button.grid(column=1, row=1)

def get_info():
    x = open_button.get()
    print (x)

button1 = ttk.Button(text='Get Information', command=get_info)

button1.grid(column=0, row=2)

What I'm trying to do is to store the DataFrame created in open_file() to use it in get_info(). I'm getting:

AttributeError: 'Button' object has no attribute 'get'

How could I access the DataFrame created in open_button?

Upvotes: 0

Views: 384

Answers (1)

thord
thord

Reputation: 36

This might help you along the way. You can set a tk.StringVar then use to retrieve, store, and access items that are input through various tk/ttk widgets. Also I suppose you might want to store your inbound file? I ran this attempt simply reading in the csv, not using pandas.

import tkinter as tk
from tkinter import filedialog, ttk
#import pandas as pd

window = tk.Tk()
window.geometry('350x240')
tkvar1 = tk.StringVar(window)

def open_file():
    temp_file = filedialog.askopenfilename(title="Open file", filetypes=[("Excel files", "*.csv")])
    temp_file = open(temp_file, "r")
    tkvar1.set(temp_file.read())
    #Proj_df = pd.read_csv(temp_file)

open_button = ttk.Button(text='Select File...', command=open_file)
open_button.grid(column=1, row=1)

def get_info():
    x = tkvar1.get()
    print (x)

button1 = ttk.Button(text='Get Information', command=get_info)

button1.grid(column=0, row=2)
window.mainloop()

Upvotes: 2

Related Questions