2

I am using Tkinter in python 3.4 to make a text based game, and I cannot figure out how to get a string from an Entry widget, it just returns Py_Var#, # being a number. I have looked at answers to similar questions, but none of them quite line up with what I need and have. Here's the relevant pieces of code:

from tkinter import * 

win = Tk() 
win.geometry("787x600")

playername = StringVar()

def SubmitName():
    playername.get
    #messagebox.showinfo("Success", playername)
    print(playername)

frame3 = Frame(win) 
frame3.pack()
label1 = Label(frame3, text="You awaken in a room, with no memories of yourself or your past. ")

label2 = Label(frame3, text="First, how about you give yourself a name:")

label1.config(font=("Courier", 11)) 
label2.config(font=("Courier", 11))

entry1 = Entry(frame3, textvariable=playername) 
entry1.config(font=("Courier", 11))

label1.grid(row=0, column=0, columnspan=3) 
label2.grid(row=1, column=0)

entry1.grid(row=1, column=1)

bnamesub= Button(frame3, text="Submit", command=lambda: SubmitName()) 
bnamesub.grid()

win.mainloop()

Also, first time using stackoverflow and its reading weird but w/e.

3 Answers 3

3

You have two mistakes in SubmitName().

First, you need to get the text like this:

txt = playername.get()

Then you need to print that txt:

print(txt)

By mistake you printed the StringVar variable itself.

Sign up to request clarification or add additional context in comments.

Comments

1
from tkinter import *
import pickle

win = Tk()
win.geometry("787x600")

def SubmitName():
        playername = entry1.get()
        messagebox.showinfo("Success", playername)
        print(playername)

frame3 = Frame(win)
frame3.grid()
label1 = Label(frame3, text="You awaken in a room, with no memories of yourself or your past. ")

label2 = Label(frame3, text="First, how about you give yourself a name:")

label1.config(font=("Courier", 11))
label2.config(font=("Courier", 11))

#name entered is a StringVar, returns as Py_Var7, but I need it to return the   name typed into entry1.
entry1 = Entry(frame3)
entry1.config(font=("Courier", 11))

label1.grid(row=0, column=0, columnspan=3)
label2.grid(row=1, column=0)

entry1.grid(row=1, column=1)

bnamesub= Button(frame3, text="Submit", command=lambda: SubmitName())
bnamesub.grid()

What I changed:
-deleted playername = StringVar(). We don't really need it;
-changed inside the function: changed playername.get to playername = entry1.get();
-added frame3.grid() (without geometry managment, widgets cannot be shown on the screen.);
-also, a little edit: in Python, comments are created with # sign. So I changed * to #.

1 Comment

Woohoo! Thank you Parviz, that got it. everywhere i went about this had slightly different answers that don't line up with eachother, thanks so much.
0

I was happy to find a solution here, but all these answers "as it is" are not working with my setting, python3.8, pycharm 2018.2 So if anyone could answer this, it seems that entry1.get() cannot be used as a string. I first wanted to append it in a list, and I did a more simple version to point out the trouble :

from tkinter import *
import pickle

win = Tk()
win.geometry("300x300")
#playername = StringVar()

def SubmitName():
    labell = Label(win, text="Little tryup").grid()
    playername = entry1.get()
    # result about line 11: 'NoneType' object has no attribute 'get'
    labelle = Label(win, text=playername).grid()
#    print(txt)

label1 = Label(win, text="Enter a name:").grid()
entry1 = Entry(win).grid()
boutonne = Button(win, text="label-it!", command=lambda: SubmitName())
boutonne.grid()
win.mainloop()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.