【发布时间】:2020-04-15 18:28:56
【问题描述】:
我正在尝试使用 Entry 小部件来获取用户的数据,然后将其打印出来。 Why is Tkinter Entry's get function returning nothing? 这对我没有帮助。
这是我的代码
message = ''
# start_chatting function
def start_chatting ():
global message
master2 = tk.Tk()
master2.geometry("1280x720")
master2.title("Messenger")
label = tk.Label(master2, text = "Messenger!!!",bg = '#1e00ff',fg ='yellow',width = 35, height = 5).place(x = 500, y = 0)
username_label = tk.Label(master2,text = usernames[position_counter],bg = '#91806d',fg ='white',width = 10, height = 2).place(x = 0, y = 100)
v = StringVar()
L1 = Label(master2, text = "Type your message : ").place(x=0, y = 680)
e = Entry(master2,textvariable = v)
e.insert(END, '')
e.pack()
e.place(x = 115, y = 680)
submit_button = Button(master2,text = "Submit",command = submit_f).place(x = 200, y = 680)
message = message+ v.get()
master2.mainloop()
#submit_f function
def submit_f ():
global message
print(message)
请记住,这是我的代码的一部分,而不是全部。
提前致谢!
【问题讨论】:
-
e.get()是一回事,v.get()显然是另一种野兽。在您的代码中,您调用的是StringVar的get方法,而不是Entry的 -
我试过 e.get() 但我得到了相同的结果。
-
好吧,如果尝试检索该值的唯一代码是
message = message+ v.get(),那么这将在 GUI 出现之前执行,因此您甚至没有机会输入任何内容。每次按下按钮时,您的submit_f应该是来自Entry的getting 数据 -
代码中的缩进看起来坏了。
标签: python-3.x tkinter tkinter-entry