[英]Disable default keyboard shortcuts for tkinter text element
I am making a basic code editor with tkinter in python, and I would like ctrl+/
to comment out the selected code.我正在用 python 中的 tkinter 制作一个基本的代码编辑器,我想
ctrl+/
注释掉所选的代码。 The problem is, ctrl+/
selects all the text in the element.问题是,
ctrl+/
选择元素中的所有文本。
I would like to know if there is a way to disable or work around this, as well as other default shortcuts/bindings, such as ctrl+a
?我想知道是否有办法禁用或解决此问题,以及其他默认快捷方式/绑定,例如
ctrl+a
?
Here is the code that I have so far:这是我到目前为止的代码:
import tkinter as tk
import ctypes
def comment_code() -> None:
## This does not change the selection range, however, when ctrl+/ is pressed, all the text is selected
## With other key combinations the problem does not occur
## Get the selection range
selection = text.tag_ranges('sel')
if not selection: ## If there is no selection
## Todo: Code to comment out lines
return
else:
## Add '\*' and '*\' to the start and end of the selected text
text.replace(selection[0], selection[1], f'\\* {text.selection_get()} *\\')
window = tk.Tk()
## Increase the scaling and the DPI for the window. Increasing the DPI makes the resolution higher but decreases the size of the window and increasing the scale increases the size of the window.
ctypes.windll.shcore.SetProcessDpiAwareness(2)
window.tk.call('tk', 'scaling', 1.9)
## Make the first row and column resize with the window
window.columnconfigure(0, weight=1)
window.rowconfigure(0, weight=1)
## Set the window's size and position
window.geometry('720x480+720+480')
## Create a text element
text = tk.Text()
text.grid(sticky='NSWE')
## Bind ctrl+/ to comment out code
text.bind('<Control-/>', lambda e: comment_code())
## Insert sample text
text.insert(1.0, 'Lorem ipsum dolor sit amet consectetur, adipisicing elit. Odio delectus impedit consequatur laborum, ducimus cum perferendis iste sed officiis quis quisquam id cupiditate nemo nobis, qui ut mollitia iusto nulla.')
window.mainloop()
You can disable the system default handling by returning "break" from the bind callback:您可以通过从绑定回调中返回“break”来禁用系统默认处理:
def comment_code() -> None:
## This does not change the selection range, however, when ctrl+/ is pressed, all the text is selected
## With other key combinations the problem does not occur
## Get the selection range
selection = text.tag_ranges('sel')
if not selection: ## If there is no selection
## Todo: Code to comment out lines
pass
else:
## Add '\*' and '*\' to the start and end of the selected text
text.replace(selection[0], selection[1], f'\\* {text.selection_get()} *\\')
# disable system default handling
return "break"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.