V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
bulletmarquis
V2EX  ›  Python

求助:在 Tkinter 的 Entry 上如何监听类似 pyqt 的 textChanged 事件?

  •  
  •   bulletmarquis · 2017-03-27 13:44:23 +08:00 · 2667 次点击
    这是一个创建于 2587 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我目前有一个需求,当某个 entry 里面 text 变更的时候,需要根据当前的 text 联动修改其他 widget

    但是使用 bind 按键事件的时候, bind 的触发时用 get 方法取到的还是这个 text 变动之前的值,即 bind 的事件是在 text 的值修改之前就触发的

    样例代码如下:

    from tkinter import *
    
    def printkey(event):
        print(u'你按下了: ' + event.char)
        print(u'当前字符:' + entry.get())
        print
    
    root = Tk()
    inputText = StringVar()
    
    entry = Entry(root, textvariable=inputText)
    
    entry.bind('<Key>', printkey)
    
    entry.pack()
    root.mainloop()
    

    执行的时候,我在entry里面依次敲了A、B、C三个按键 从print的值里能看到,用get方法只能获取到敲键之前的text的值 第一轮:敲击“A”,但是get到的字符串是空的 第二轮:继续敲击“B”,但是get到的字符串是“A” 第三轮:继续敲击“C”,但是get到的字符串是“AB”

    有什么方式可以get到当前entry中的字符串呢?

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1101 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:17 · PVG 07:17 · LAX 16:17 · JFK 19:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.