我正在使用Text小部件,我对Tk使用的旧式快捷方式有疑问。
即:
全选:Ctrl + /
vs Ctrl + a
剪切:Ctrl + w
vs Ctrl + x
复制:Meta + w
vs Ctrl + c
粘贴:Ctrl + y
vs Ctrl + v
在Windows上,除Ctrl + a外,所有这些都有效。
1)是否可以重定向绑定,因此.bind('<Control-a>')
调用已绑定Ctrl + /?
2)我尝试了“全选”:
txt_text.bind('<Control-a>', self.ctext_selectall)
其中:
def ctext_selectall(self, callback):
"""Select all text in the text widget"""
self.txt_text.tag_add('sel', '1.0', 'end')
但它不起作用,因为Ctrl + a默认工作(光标转到开头)。它与其他一些未绑定的字母一起使用。如果不能解决1以下的解决方案,是否有机会使这项工作?
答案 0 :(得分:11)
默认绑定应用于窗口小部件类。当您执行绑定时,它会影响特定的窗口小部件,并且绑定在类绑定之前发生。所以正在发生的事情是你的绑定正在发生,然后发生类绑定,这使得它看起来好像你的绑定不起作用。
有两种方法可以解决这个问题。一,你的ctext_selectall
可以返回字符串“break”,这将阻止类绑定被触发。这应该足以解决你当前的问题。
第二个解决方案涉及更改类绑定,以便您的首选绑定适用于所有文本小部件。您可以使用bind_class
方法执行此操作。
以下是重新绑定课程的示例:
def __init__(...):
self.root.bind_class("Text","<Control-a>", self.selectall)
def selectall(self, event):
event.widget.tag_add("sel","1.0","end")
effbot.org有一篇相当不错的文章,名为Events and Bindings。它详细介绍了类和窗口小部件绑定及其发生的顺序。
Tk的绑定机制是所有GUI工具包中最好的。一旦你理解它是如何工作的(并且它非常简单),你会发现很容易扩充或替换任何或所有默认绑定。
答案 1 :(得分:3)
您可以随意使用以下代码,或至少查看select_all
和DiacriticalEntry
类中DiacriticalText
方法的实现方式。如果您选择使用这些类来代替您当前使用的任何小部件,您还将获得这样的优势,即用户可以轻松键入某些特别难以输入的字符。
## {{{ http://code.activestate.com/recipes/576950/ (r3)
from tkinter import *
from tkinter.scrolledtext import ScrolledText
from unicodedata import lookup
import os
class Diacritical:
"""Mix-in class that adds keyboard bindings for accented characters, plus
other common functionality.
An inheriting class must define a select_all method that will respond
to Ctrl-A."""
accents = (('acute', "'"), ('grave', '`'), ('circumflex', '^'),
('tilde', '='), ('diaeresis', '"'), ('cedilla', ','),
('stroke', '/'), ('ring above', ';'))
def __init__(self):
# Fix some key bindings
self.bind("<Control-Key-a>", self.select_all)
# We will need Ctrl-/ for the "stroke", but it cannot be unbound, so
# let's prevent it from being passed to the standard handler
self.bind("<Control-Key-/>", lambda event: "break")
# Diacritical bindings
for a, k in self.accents:
# Little-known feature of Tk, it allows to bind an event to
# multiple keystrokes
self.bind("<Control-Key-%s><Key>" % k,
lambda event, a=a: self.insert_accented(event.char, a))
def insert_accented(self, c, accent):
if c.isalpha():
if c.isupper():
cap = 'capital'
else:
cap = 'small'
try:
c = lookup("latin %s letter %c with %s" % (cap, c, accent))
self.insert(INSERT, c)
# Prevent plain letter from being inserted too, tell Tk to
# stop handling this event
return "break"
except KeyError as e:
pass
class DiacriticalEntry(Entry, Diacritical):
"""Tkinter Entry widget with some extra key bindings for
entering typical Unicode characters - with umlauts, accents, etc."""
def __init__(self, master=None, **kwargs):
Entry.__init__(self, master, **kwargs)
Diacritical.__init__(self)
def select_all(self, event=None):
self.selection_range(0, END)
return "break"
class DiacriticalText(ScrolledText, Diacritical):
"""Tkinter ScrolledText widget with some extra key bindings for
entering typical Unicode characters - with umlauts, accents, etc."""
def __init__(self, master=None, **kwargs):
ScrolledText.__init__(self, master, **kwargs)
Diacritical.__init__(self)
def select_all(self, event=None):
self.tag_add(SEL, "1.0", "end-1c")
self.mark_set(INSERT, "1.0")
self.see(INSERT)
return "break"
def test():
frame = Frame()
frame.pack(fill=BOTH, expand=YES)
if os.name == "nt":
# Set default font for all widgets; use Windows typical default
frame.option_add("*font", "Tahoma 8")
# The editors
entry = DiacriticalEntry(frame)
entry.pack(fill=BOTH, expand=YES)
text = DiacriticalText(frame, width=76, height=25, wrap=WORD)
if os.name == "nt":
# But this looks better than the default set above
text.config(font="Arial 10")
text.pack(fill=BOTH, expand=YES)
text.focus()
frame.master.title("Diacritical Editor")
frame.mainloop()
if __name__ == "__main__":
test()
## end of http://code.activestate.com/recipes/576950/ }}}