Reputation: 343
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. 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.
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
Python 3.4.3
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. 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).
Thanks in advance
Upvotes: 1
Views: 698
Reputation: 453
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(). 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. After 1 millisecond, it goes to the display_after_value function, which displays the value of the text widget including the tab space.
Upvotes: 1