0

I am trying to hand the selected filename of my open file dialogue over to an Entry widget. All the examples I looked at simply refer to the Entry-widget within the function executed on button click. However, when I do that, the app breaks on AttributeError: 'NoneType' object has no attribute 'delete'.

I simply don't get it why the object is not treated as an Entry widget.

import tkinter as tk
from tkinter import filedialog as fd
from tkinter import ttk

global init_csv
init_csv = ""

def select_csv_file(type_csv):
    filename = fd.askopenfilename(
            title="Select csv file",
            filetypes=[("comma separated files","*.csv")]
            )
    match type_csv:
        case "init_csv":
            init_csv = filename
            err_csv.delete(0,END)

app_window = tk.Tk()
del_labelfrm = ttk.LabelFrame(app_window,text=" Thin-out errors ")
del_labelfrm.pack(fill="x",padx=4,pady=4)
ttk.Label(del_labelfrm,text="Input CSV file: ").grid(column=0,row=0,padx=5,pady=5)
err_csv = ttk.Entry(del_labelfrm,textvariable=init_csv,width=50).grid(column=1,
                                                                      row=0,
                                                                      padx=5,
                                                                      pady=5)
csv_add = tk.PhotoImage(file="./img/add_csv.png")
err_csv_btn = ttk.Button(del_labelfrm,image=csv_add,command=lambda:
select_csv_file("init_csv")).grid(column=2,row=0,pady=5)

app_window.pack_slaves()
app_window.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
TomGeo
  • 1,213
  • 2
  • 12
  • 24
  • `err_csv_btn` will be `None` because you are getting the return from `grid` which is `None`. As far as updating the button goes: you could do it in `select_csv_file` since that's what get's called when you click the button. – OneMadGypsy Dec 08 '21 at 16:07

1 Answers1

1

You are not far off. A couple of things to get your code working:

  • For some reason you need to split the widget definition and the placement in your layout if you later want to access that item properly:
err_csv = ttk.Entry(del_labelfrm,textvariable=init_csv,width=50)
err_csv.grid(column=1, row=0, padx=5, pady=5)
  • You also need to insert the text of your filename into the widget, inside your select_csv_file function
        err_csv.delete(0, END)
        err_csv.insert(0, filename)
  • And lastly, you need to import the END statement to use it
from tkinter import END

Full working example (only that i replaced your case with a simple if, do i need python 3.10 for that to work? im curious..):

import tkinter as tk
from tkinter import filedialog as fd
from tkinter import ttk
from tkinter import END

global init_csv
init_csv = ""

def select_csv_file(type_csv):
    filename = fd.askopenfilename(
            title="Select csv file",
            filetypes=[("comma separated files","*.csv")]
            )
    if type_csv == "init_csv":
        init_csv = filename
        err_csv.delete(0,END)
        err_csv.insert(0, filename)

app_window = tk.Tk()
del_labelfrm = ttk.LabelFrame(app_window,text=" Thin-out errors ")
del_labelfrm.pack(fill="x",padx=4,pady=4)
ttk.Label(del_labelfrm,text="Input CSV file: ").grid(column=0,row=0,padx=5,pady=5)
err_csv = ttk.Entry(del_labelfrm,textvariable=init_csv,width=50)
err_csv.grid(column=1, row=0, padx=5, pady=5)
csv_add = tk.PhotoImage(file="./img/add_csv.png")
err_csv_btn = ttk.Button(del_labelfrm,image=csv_add,command=lambda:
select_csv_file("init_csv")).grid(column=2,row=0,pady=5)

app_window.pack_slaves()
app_window.mainloop()
mnikley
  • 1,625
  • 1
  • 8
  • 21