使用带有 tkinter 的 Python 3 选择文本小部件中的所有文本 [英] Select all text in a Text widget using Python 3 with tkinter

查看:42
本文介绍了使用带有 tkinter 的 Python 3 选择文本小部件中的所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写我的第一个 Python 程序,但对我在做什么几乎一无所知.我想重新绑定 ctrl-a (control a) 以选择 Text 小部件中的所有文本.当前绑定是ctrl-/(控制/).绑定部分直接跳转到函数,但实际的文本选择不起作用.相反,光标会跳转到第一行的第一个字符(就像它应该的那样)并且没有其他任何事情发生.我确信这很容易解决,但在花了一个小时一个小时后,我无法弄清楚出了什么问题.

I'm working on my first Python program and have little idea what I'm doing. I want to re-bind ctrl-a (control a) to select all text in a Text widget. The current binding is ctrl-/ (control /). The binding part jumps right to the function but the actual text selection doesn't work. Instead, the cursor jumps to the first character on the first line (like it should) and nothing else happens. I'm sure this is embaressingly easy to fix but after spending hour an hours on it, I can't figure out what's wrong.

Python 3,Windows:

Python 3, Windows:

from tkinter import *

# Select all the text in textbox (not working)
def select_all(event):
    textbox.tag_add(SEL, "1.0", END)
    textbox.mark_set(INSERT, "1.0")
    textbox.see(INSERT)

# Open a window
mainwin = Tk()

# Create a text widget
textbox = Text(mainwin, width=40, height=10)
textbox.pack()

# Add some text
textbox.insert(INSERT, "Select some text then right click in this window")

# Add the binding
textbox.bind("<Control-Key-a>", select_all)

# Start the program
mainwin.mainloop()

推荐答案

您需要先进行选择,然后通过让函数返回字符串break"来禁止默认操作.

You need to both do the selection and then inhibit the default action by having your function return the string "break".

这是由于 Tkinter 处理事件的方式.它使用所谓的绑定标签".尽管看起来您正在绑定到一个小部件,但实际上您正在绑定到一个作为小部件名称的标签.还可以绑定到小部件类、小部件所在的顶级窗口以及标签all"(此外,如果您愿意,您可以创建自己的标签).

This is due to how Tkinter processes events. It uses what it calls "bind tags". Even though it looks like you are binding to a widget, you are actually binding to a tag that is the name of the widget. There can also be bindings to the widget class, to the toplevel window that the widget is in, and the tag "all" (plus, you can invent your own tags if you wish).

这些标签的默认顺序是从最具体到最不具体,事件按该顺序处理.意思是,如果您在小部件(最具体)和类(不太具体)上都有绑定,则绑定将首先针对小部件触发,然后针对类绑定(然后针对顶层,然后针对所有").

The default ordering of these tags is from most-specific to least-specific, and events are processed in that order. Meaning, if you have a binding both on the widget (most specific) and the class (less specific), the binding will fire for the widget first, and then for the class binding (and then for the toplevel, and then for "all").

这意味着默认情况下,小部件上的绑定增强而不是替换默认绑定.好消息是,您可以通过简单地返回字符串break"来阻止任何进一步的绑定触发,这会阻止绑定链做更多的工作.

What this means is that by default, a binding on a widget augments rather than replaces a default binding. The good news is, you can inhibit any further bindings from firing by simply returning the string "break", which stops the chain of bindings from doing any more work.

这篇关于使用带有 tkinter 的 Python 3 选择文本小部件中的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆