简体   繁体   English

在Tkinter中按下Tab键后,如何捕获文本小部件的值?

[英]How to Capture the Value of a Text Widget after the Tab Key is Pressed in Tkinter?

I have a sample script (shown below) in which I am simply trying to capture the value of a tkinter text widget everytime the "Tab" key is pressed. 我有一个示例脚本(如下所示),其中每次按下“ Tab”键时,我只是试图捕获tkinter文本小部件的值。 Two functions are used to help with this. 有两个功能可以帮助解决这个问题。 One should run and show the value of the text widget before a Tab changes the value. 在选项卡更改值之前,应运行并显示文本小部件的值。 The other function should run and show the value of the text widget after the Tab changes the value. Tab更改值后,另一个函数应运行并显示文本小部件的值。

The Problem: 问题:

The problem is that only one function runs -- the function that displays the value of the text widget before the tab changes its value. 问题是只有一个函数运行-该函数在选项卡更改其值之前显示文本窗口小部件的值。

My System: 我的系统:

Ubuntu 12.04 Ubuntu 12.04

Python 3.4.3 的Python 3.4.3

Tk 8.5 Tk 8.5

The Code: 编码:

import tkinter as tk

def display_before_value(value):
        """Display the value of the text widget before the class bindings run"""
        print("The (before) value is:", value)
        return


def display_after_value(value):
        """Display the value of the text widget after the class bindings run"""
        print("The (after) value is:", value)
        return


# Add the widgets
root = tk.Tk()
text = tk.Text(root)

# Add "post class" bindings to the bindtags
new_bindings = list(text.bindtags())
new_bindings.insert(2, "post-class")
new_bindings = tuple(new_bindings)
text.bindtags(new_bindings)
# Show that the bindtags were updated
text.bindtags()
# Outputs ('.140193481878160', 'Text', 'post-class', '.', 'all')

# Add the bindings
text.bind("<Tab>", lambda e: display_before_value(text.get("1.0", tk.END)))
text.bind_class("post-class", "<Tab>", lambda e: display_after_value(text.get("1.0", tk.END)))

# Show the text widget
text.grid()

# Run
root.mainloop()

Running the above code in the command line/terminal will only show the output of the display_before_value() function. 在命令行/终端中运行上述代码将仅显示display_before_value()函数的输出。 So I'm assuming that the post-class bindings are not working for some reason. 因此,我假设类后绑定由于某种原因无法正常工作。 However, if I change the bindings from <Tab> to <Key> then both display_before_value() and display_after_value() run correctly when I type any key in the text widget (except the Tab key of course). 但是,如果我将绑定从<Tab>更改为<Key>则当我在文本小部件中键入任何键(当然,Tab键除外)时display_before_value()display_after_value()都可以正确运行。

Thanks in advance 提前致谢

If you want the text to be shown before the tab space and the text to be shown with the tab space after, try using root.after(). 如果希望文本在选项卡空间之前显示,文本在选项卡空间之后显示,请尝试使用root.after()。 Here is an example with your code: 这是您的代码的示例:

import tkinter as tk

def display_before_value(event):
        """Display the value of the text widget before the class bindings run"""
        value = text.get("1.0", tk.END)
        print("The (before) value is:", value)
        root.after(1, display_after_value)
        return

def display_after_value():
        """Display the value of the text widget after the class bindings run"""
        value = text.get("1.0", tk.END)
        print("The (after) value is:", value)
        return

# Add the widgets
root = tk.Tk()
text = tk.Text(root)

# Add the bindings
text.bind("<Tab>", display_before_value)

# Show the text widget
text.grid()

# Run
root.mainloop()

When the tab key is pressed, the display_before_value function is executed, which prints the value of the text widget without the tab space in it. 当按下Tab键时,将执行display_before_value函数,该函数将打印文本小部件的值,其中不包含标签空间。 After 1 millisecond, it goes to the display_after_value function, which displays the value of the text widget including the tab space. 1毫秒后,它将转到display_after_value函数,该函数显示文本小部件的值,包括制表符空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在将文本输入文本小部件后按Enter键时如何执行回调? - How do I execute a callback when the Enter key is pressed after entering text into a Text widget? 在 Tkinter 中按下按钮后如何清除 Entry 小部件? - How to clear the Entry widget after a button is pressed in Tkinter? 如何在 tkinter 的文本小部件中设置值? - how to set value in Text widget in tkinter? 如何在python Tkinter中按下按钮后显示文本 - how to display text after a button is pressed in python Tkinter 即使在 TKinter (python) 中按下 Enter 键,如何生成 Tab 键? - How to generate tab key even when enter key pressed in TKinter (python)? 如何在 tkinter 中按下按钮后立即更改值? - How to get a value to change immediately after a button is pressed in tkinter? 调整大小后如何获取Tkinter Text小部件的高度? - How to get the height of the Tkinter Text widget after resizing? tkinter 文本小部件:如何在软换行后自动缩进 - tkinter Text Widget: how to indent automatically after a soft line wrap 在笔记本中添加新标签后,Tkinter文本小部件行号消失 - Tkinter text widget line numbers dissapear after adding new tab in Notebook 如何使用 tkinter 中的按钮设置“Entry”小部件的文本/值/内容 - How to set the text/value/content of an `Entry` widget using a button in tkinter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM