Python tkinter entry get() not returning a value

The following coding executes perfectly. However when I make it into a function within a larger program with the first line reading: def get_filename_changes(): the prefix_var.get() in the done_clicked() function no longer gets the data that I entered. I tried declaring var_get as a global variable but that made not difference.
I would greatly appreciate a solution to this problem.

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo

# root window
root = tk.Tk()
root.geometry("500x400")
root.resizable(False, False)
root.title('Modify new filenames')

prefix = tk.StringVar()
insertion = tk.StringVar()
deletion = tk.StringVar

def done_clicked():
  prefix_value = prefix.get()
  msg = f'You entered : {prefix_value}'
  showinfo(title='Information', message=msg)
  return

change_filename = ttk.Frame(root)
change_filename.pack(padx=10, pady=10, fill='x', expand=True)

prefix_label = ttk.Label(change_filename, text="Count of filename characters to be moved from prefix to suffix:")
prefix_label.pack(fill='x', expand=True)

prefix_entry = ttk.Entry(change_filename, textvariable=prefix)
prefix_entry.pack(fill='x', expand=True)
prefix_entry.focus()

done_button = ttk.Button(change_filename, text="Done", command=done_clicked)
done_button.pack(fill='x', expand=True, pady=10)

root.mainloop()
1 Like

I’ve edited your post for readability. I hope the code is correct, but I can’t guarantee that and it may need some further formatting – apologies if that is the case! Python is whitespace-sensitive, so just pasting every line without whitespace, as was done, results in broken code, which in turn makes it difficult to provide help.

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Thank you for your attention. Here is my coding again.

# This is a stripped down Python program for getting input from the user.
# It recognizes the input from the user.
# However when this same coding is inserted as a function in a large program
# it no longer recognizes the input from the user.

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo

# root window
root = tk.Tk()
root.geometry("500x400")
root.resizable(False, False)
root.title('Modify new filenames')
prefix = tk.StringVar()

def done_clicked():
    prefix_value = prefix.get()            # pick up what the user entered
    print("You entered " + str(prefix_value))  
    
change_filename = ttk.Frame(root)
change_filename.pack(padx=10, pady=10, fill='x', expand=True)

prefix_label = ttk.Label(change_filename, text="Count of filename prefix characters")
prefix_label.pack(fill='x', expand=True)
prefix_entry = ttk.Entry(change_filename, textvariable=prefix)
prefix_entry.pack(fill='x', expand=True)
prefix_entry.focus()

# Done button
done_button = ttk.Button(change_filename, text="Done", command=done_clicked)
done_button.pack(fill='x', expand=True, pady=10)

root.mainloop()

Looks like you left out the “entry” part of your prefix variable name. So your prefix_value = prefix.get() needs to read prefix_value = prefix_entry.get()

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.