Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter条目/文本小部件在使用绑定函数时出现问题_Python_Events_Tkinter_Tkinter Entry - Fatal编程技术网

Python Tkinter条目/文本小部件在使用绑定函数时出现问题

Python Tkinter条目/文本小部件在使用绑定函数时出现问题,python,events,tkinter,tkinter-entry,Python,Events,Tkinter,Tkinter Entry,我是Tkinter的新手,我想在打字时打印条目的内容。 以下是我尝试过的代码: from tkinter import * def get_(e): print(entry.get()) root = Tk() entry = Entry(root) entry.pack() entry.bind("<KeyPress>", get_) mainloop() 从tkinter导入* def get_(e): 打印(entry.get()) root=Tk() 条目

我是Tkinter的新手,我想在打字时打印条目的内容。 以下是我尝试过的代码:

from tkinter import *


def get_(e):
    print(entry.get())

root = Tk()
entry = Entry(root)
entry.pack()

entry.bind("<KeyPress>", get_)

mainloop()
从tkinter导入*
def get_(e):
打印(entry.get())
root=Tk()
条目=条目(根)
entry.pack()
entry.bind(“,get\ux”)
mainloop()
但它似乎不“同步”(当我在中键入“123”时,输出仅为“12”,以此类推)

以下代码工作正常,但我不知道原因:

from tkinter import *


def get_(e):
    print(entry.get())

root = Tk()
entry = Entry(root)
entry.pack()

root.bind("<KeyPress>", get_)
## or this: entry.bind("<KeyRelease>", get_)
## or this: entry.bind_all("<KeyPress>", get_)

mainloop()
从tkinter导入*
def get_(e):
打印(entry.get())
root=Tk()
条目=条目(根)
entry.pack()
root.bind(“,get\ux”)
##或者这个:entry.bind(“,get_”)
##或者这个:entry.bind\u all(“,get\u”)
mainloop()
有什么奇怪的规则我不知道吗?任何和所有的帮助都会很好,提前谢谢

问题
entry.bind(“
似乎不“同步”(当我在输出中键入
“123”
时,仅输出
“12”
等…),而
root.bind(“
有效)

事件
entry.bind(“,…
tk.entry
中的值更新之前被激发。这解释了为什么输出总是一个字符落后

更新
tk.Entry
中的值后,
root.bind(“,…
被激发事件。这解释了为什么这样做

备选方案

  • 使用
    事件

参考


虽然不是完全重复的,但这包含了您需要的所有信息:@BryanOakley谢谢您的评论。我阅读了您的答案和代码。所以像“Entry”这样的绑定标记所做的就是在小部件中插入一些字符。我说的对吗?@Darcy:“在我键入时打印条目的内容”:您想要吗