Search code examples
pythontkinterttk

How to update TKinter Entry on button click


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()

Solution

  • 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()