简体   繁体   English

Python / Tkinter:构建一个工具栏,提供编辑剪切,复制,粘贴命令

[英]Python/Tkinter: Building a toolbar that provides edit cut, copy, paste commands

I'm looking for suggestions on how one might implement a toolbar that provides edit cut, copy, paste commands using the Tkinter framework. 我正在寻找有关如何实现使用Tkinter框架提供编辑剪切,复制,粘贴命令的工具栏的建议。 I understand how to build a toolbar and bind the toolbar commands, but I'm confused over how the toolbar button bound commands will know which widget to apply the cut, copy, or paste action because the widget with edit activity will lose focus when the toolbar button is clicked. 我了解如何构建工具栏并绑定工具栏命令,但是我对工具栏按钮绑定命令如何知道要应用剪切,复制或粘贴操作的窗口小部件感到困惑,因为具有编辑活动的窗口小部件在打开时会失去焦点。单击工具栏按钮。 My first thought was to have each widget with potential edit activity set a global variable when the widget gains focus and have other widgets (without edit activity, eg. buttons, sliders, checkbox/radiobox, etc) clear this global variable. 我的第一个想法是让每个具有潜在编辑活动的窗口小部件在获得焦点时设置一个全局变量,并让其他窗口小部件(无编辑活动,例如按钮,滑块,复选框/单选框等)清除此全局变量。 But this sounds complicated to maintain unless I build a framework of widgets that inherit this behavior. 但这听起来很难维护,除非我建立一个继承此行为的小部件框架。

Is there a simpler way to go about this or am I on the right track? 有没有更简单的方法可以解决此问题,或者我走在正确的轨道上?

You don't have to maintain a big framework, you can create a single binding on the root widget for <FocusIn> and put all the logic in that binding. 您不必维护大型框架,可以在<FocusIn>的根窗口小部件上创建单个绑定,并将所有逻辑放入该绑定中。 Or, use focus_class and bind to the class all . 或者,使用focus_class并绑定到all类。

Binding on the root will only affect children of the root, binding to all will affect all widgets in the entire app. 在根目录上的绑定只会影响根目录的子级,而对all根目录的绑定都会影响整个应用程序中的所有小部件。 That only matters if you have more than one toplevel widget. 这仅在您拥有多个顶级小部件时才重要。

You can tell the toolbar buttons to not take the focus; 您可以告诉工具栏按钮不要聚焦。 it's a configuration option and no UI guidelines I've ever seen have had toolbar buttons with focus. 这是一个配置选项,我见过的UI指南都没有集中显示工具栏按钮。 (Instead, the functionality is always available through some other keyboard-activatable mechanism, eg, a hotkey combo.) (相反,该功能始终可以通过其他一些键盘可激活的机制(例如热键组合)使用。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM