1

(Edit: Final working code below!)

I have been trying to do a tkinter text box with three inputs at the beginning of a code.

My main objective is to retrieve these three inputs as strings, which I would be able to integrate with the rest of the code (anything that goes beyond the root.mainloop()).

I am able to to the tkinter basics, but I am unable to integrate the user response with the other part of the code.

import tkinter as tk
from tkinter import ttk
   
def close_window():
    root.destroy()

def show_entry_fields():
    print("First %s\nSecond %s\nThird %s" % (e1.get(), e2.get(), e3.get()))
    firstinput = e1.get()
    secondinput = e2.get()
    thirdinput = e3.get()

root = tk.Tk()

tk.Label(root, text="First input").grid(row=0)
tk.Label(root, text="Second input").grid(row=1)
tk.Label(root, text="Third input").grid(row=2)

text1 = tk.StringVar()
text2 = tk.StringVar()
text3 = tk.StringVar()

e1 = tk.Entry(root, textvariable=text1)
e1.grid(row=0, column=1)

e2 = tk.Entry(root, textvariable=text2)
e2.grid(row=1, column=1)

e3 = tk.Entry(root, textvariable=text3)
e3.grid(row=2, column=1)

tk.Button(root, text='Enter', command=show_entry_fields).grid(row=3, 
                                                               column=1, 
                                                               sticky=tk.W, 
                                                               pady=4)

tk.Button(root, text='Close', command=close_window).grid(row=4,
                                                         column=1,
                                                         sticky=tk.W,
                                                         pady=4)

root.mainloop()

firstinput
print(firstinput)

In that sense, my objective is to be able to use the firstinput, secondinput and thirdinput as the code goes on beyond the root.mainloop().

However, when I run this code, I get the following error:

NameError                                 Traceback (most recent call last)
<ipython-input-1-76b7cc4daea7> in <module>
     42 root.mainloop()
     43 
---> 44 firstinput

NameError: name 'firstinput' is not defined

If I can't callback upon those strings I defined in def show_entry_fields(), how can I use them in the remaining of the code?

I'm sorry if this sounded confusing, I'll be happy to better/further explain myself if necessary.

If someone can shed a light upon this, I'll be ever so grateful. Many thanks.

EDIT: With the help of the comments, here is the final (and working) version:

import tkinter as tk
from tkinter import ttk

def close_window():
    root.destroy()

def show_entry_fields():
    print("First %s\nSecond %s\nThird %s" % (e1.get(), e2.get(), e3.get()))

root = tk.Tk()

tk.Label(root, text="First input").grid(row=0)
tk.Label(root, text="Second input").grid(row=1)
tk.Label(root, text="Third input").grid(row=2)

text1 = tk.StringVar()
text2 = tk.StringVar()
text3 = tk.StringVar()

e1 = tk.Entry(root, textvariable=text1)
e1.grid(row=0, column=1)

e2 = tk.Entry(root, textvariable=text2)
e2.grid(row=1, column=1)

e3 = tk.Entry(root, textvariable=text3)
e3.grid(row=2, column=1)

tk.Button(root, text='Enter', command=show_entry_fields).grid(row=3, 
                                                               column=1, 
                                                               sticky=tk.W, 
                                                               pady=4)

tk.Button(root, text='Close', command=close_window).grid(row=4,
                                                         column=1,
                                                         sticky=tk.W,
                                                         pady=4)

root.mainloop()

firstinput = text1.get()
secondinput = text2.get()
thirdinput = text3.get()
print(firstinput)
mmurer
  • 15
  • 3
  • `print()` returns `None`; therefore `str(print(anything))` will return `"None"`. You don't need either of those functions, `.get()` returns a string already. – jasonharper Jul 07 '22 at 19:48
  • Hello! I have adjusted the code based on your comment. Although I believe it's more correct now, I still don't have the desired outcome. Many thanks anyway :) – mmurer Jul 07 '22 at 22:13
  • 1
    `firstinput` is a local variable inside `show_entry_fields()`, so it cannot be accessed outside the function. For your case, use `text1.get()` instead. – acw1668 Jul 07 '22 at 23:03
  • In line 44 remove it. – toyota Supra Jul 08 '22 at 01:42

0 Answers0