Python Tkinter StringVar 只显示 Py_Var(number)
Python Tkinter StringVar only displaying Py_Var(number)
我在 python 3.4 中使用 Tkinter 制作基于文本的游戏,但我不知道如何从 Entry 小部件获取字符串,它只是 returns Py_Var#
, # 是一个数字。我已经查看了类似问题的答案,但其中 none 完全符合我的需要和拥有的内容。这是相关的代码片段:
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()
此外,第一次使用 Whosebug,它的阅读很奇怪,但是 w/e。
你在 SubmitName()
中有两个错误。
首先,你需要得到这样的文本:
txt = playername.get()
然后你需要打印 txt
:
print(txt)
错误地打印了 StringVar
变量本身。
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()
我改变了什么:
-删除 playername = StringVar()
。我们真的不需要它;
- 在函数内部更改:将 playername.get
更改为 playername = entry1.get()
;
-添加frame3.grid()
(没有几何管理,小部件无法在屏幕上显示。);
- 另外,稍微编辑一下:在 Python 中,评论是用 #
符号创建的。所以我把 *
改成 #
.
我很高兴在这里找到解决方案,但是所有这些答案 "as it is" 都不适用于我的设置,python3.8,pycharm 2018.2
所以如果有人能回答这个问题,似乎 entry1.get() 不能用作字符串。我首先想把它附加到一个列表中,我做了一个更简单的版本来指出问题:
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()
我在 python 3.4 中使用 Tkinter 制作基于文本的游戏,但我不知道如何从 Entry 小部件获取字符串,它只是 returns Py_Var#
, # 是一个数字。我已经查看了类似问题的答案,但其中 none 完全符合我的需要和拥有的内容。这是相关的代码片段:
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()
此外,第一次使用 Whosebug,它的阅读很奇怪,但是 w/e。
你在 SubmitName()
中有两个错误。
首先,你需要得到这样的文本:
txt = playername.get()
然后你需要打印 txt
:
print(txt)
错误地打印了 StringVar
变量本身。
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()
我改变了什么:
-删除 playername = StringVar()
。我们真的不需要它;
- 在函数内部更改:将 playername.get
更改为 playername = entry1.get()
;
-添加frame3.grid()
(没有几何管理,小部件无法在屏幕上显示。);
- 另外,稍微编辑一下:在 Python 中,评论是用 #
符号创建的。所以我把 *
改成 #
.
我很高兴在这里找到解决方案,但是所有这些答案 "as it is" 都不适用于我的设置,python3.8,pycharm 2018.2 所以如果有人能回答这个问题,似乎 entry1.get() 不能用作字符串。我首先想把它附加到一个列表中,我做了一个更简单的版本来指出问题:
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()