推荐学习书目
› 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 · Mar 27, 2017 · 3768 views
    This topic created in 3475 days ago, the information mentioned may be changed or developed.

    我目前有一个需求,当某个 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中的字符串呢?

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2400 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 11:32 · PVG 19:32 · LAX 04:32 · JFK 07:32
    ♥ Do have faith in what you're doing.