文本变量不起作用 [英] TextVariable not working

查看:26
本文介绍了文本变量不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Tkinter 中的 Entry 小部件中获取文本.它适用于 Entry1.get(),但不适用于 textvariable

I am trying to get the Text out of an Entry widget in Tkinter. It works with Entry1.get(), but it does not work using textvariable

我做错了什么?

from Tkinter import *
master = Tk()
v = StringVar()

def Entered(p1):
    print 'Got: ', Entry1.get()
    print 'Got: ', v.get()

Entry1 = Entry(master, text = '', width = 25, textvariable = v)
Entry1.pack()
Entry1.bind('<Return>', Entered)

推荐答案

问题在于text.

如果您提供 text 参数,则 textvariable.get() 似乎不会返回任何内容.不知道是不是bug.

If you give the text argument, it seems that the textvariable.get() will return nothing. I don't know if its a bug or not.

from Tkinter import *
master = Tk()
v = StringVar()

def Entered(p1):
    print 'Got: ', Entry1.get()
    print 'Got: ', v.get()

Entry1 = Entry(master, width = 25, textvariable = v) # No text now
Entry1.pack()
Entry1.bind('<Return>', Entered)
master.mainloop()

如果你输入 asd 它返回:

If you enter asd it returns:

Got:  asd
Got:  asd

有趣的部分,如果您将条目更改为:

The interesting part that if you change the entry to:

Entry1 = Entry(master, text = 'sajt', width = 25, textvariable = v)

它仍然不会返回任何 v.get() 而不是我期望的 sajt.

It will still return nothing with v.get() not sajt as i would expect.

这篇关于文本变量不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆