从tkinter条目获取变量

从tkinter条目获取变量,tkinter,tkinter-entry,Tkinter,Tkinter Entry,我试图在tkinter中获得一个带有输入框的字符名 def create2(): global created label.config(text ='You need to have a name.') entry = tk.Entry(storywindow, text = 'Enter your name...') entry.place(relx = .01, rely = .89, relheight = .1, relwidth = .69) hero.

我试图在tkinter中获得一个带有输入框的字符名

def create2():
   global created
   label.config(text ='You need to have a name.')
   entry = tk.Entry(storywindow, text = 'Enter your name...')
   entry.place(relx = .01, rely = .89, relheight = .1, relwidth = .69)
   hero.name = entry.get
如何将用户输入绑定到变量? 谢谢

使用我的工作代码更新:

def stats():
    label.config(text = hero.name + '\n'+ hero.mclass+'\nLevel:    ' + str(hero.level)  )
    label.config(text = 'Hitpoints: '+ str(hero.hp) +'/'+str(hero.max_hp))

def create2():
    label.config(text ='You need to have a name.')
    txt = tk.Entry(storywindow)
    txt.place(relx = .01, rely = .89, relheight = .1, relwidth = .69)

def getname():
     name = txt.get()
     hero.name = name
     txt.destroy()
     submit.destroy()
     stats()

submit = tk.Button(storywindow, text= 'Submit', command = getname)
submit.place(relx = .7, rely = .89, relheight = .1, relwidth = .29)

def create():
    label.config(text ='Please choose a main class.')
    war = tk.Button(storywindow, text = 'Warrior', command = lambda:[warrior(),war.destroy(),mag.destroy(),rog.destroy(),pri.destroy()])
    war.place(relx = .01, rely = .94, relheight = .05, relwidth = .24)
    mag = tk.Button(storywindow, text = 'Mage', command =  lambda:[mage(),war.destroy(),mag.destroy(),rog.destroy(),pri.destroy()])
    mag.place(relx = .26, rely = .94, relheight = .05, relwidth = .24)
    rog = tk.Button(storywindow, text = 'Rogue', command =  lambda:[rogue(),war.destroy(),mag.destroy(),rog.destroy(),pri.destroy()])
    rog.place(relx = .51, rely = .94, relheight = .05, relwidth = .24)
    pri = tk.Button(storywindow, text = 'Priest', command =  lambda:[priest(),war.destroy(),mag.destroy(),rog.destroy(),pri.destroy()])
    pri.place(relx = .76, rely = .94, relheight = .05, relwidth = .23)

在函数
callback
中,您要查找的是
data=str(my_entry.get())
行。我给你举了一个小例子:你可以在条目中写下文本,然后按下按钮,文本将出现在底部区域

import tkinter as tk

def callback():
    data = str(my_entry.get())
    my_entry.delete("0", "end")
    print(data)
    my_text.delete('1.0', tk.END)
    my_text.insert("insert", data)

root = tk.Tk()

my_entry = tk.Entry(master=root)
my_entry.pack()

my_button = tk.Button(master=root, command=callback, text="Print text")
my_button.pack()

my_text = tk.Text(root, width=10, height=5)
my_text.insert("insert", "Hello !")
my_text.see("end")
my_text.pack()

root.mainloop()

您发布的代码中没有任何“用户输入”存在!您只是创建了条目,用户还没有时间对其进行任何操作。在用户指示他们已完成输入文本后,您必须执行
.get()
(注意括号),以及依赖于该值的任何操作-通常这将在作为按钮的
命令=
选项传递的函数中进行。您能显示所有代码吗?
条目
(在所有GUI中)不像
input()
-它不等待用户的数据,你必须使用ie.
按钮运行代码,在你将数据放入
条目
并按下
按钮
后,从
条目
获取数据。我可以将我的代码添加到其他内容中,它有500多行。事实上,这些答案都不是我自己解决的。。。不过还是谢谢你。这仍然是一项正在进行的工作,因为我正在自学Python,还处于早期阶段。实际上,我已经让它工作了,我将用我的工作代码块更新我的代码