Python/Kivy:如何在 ctrl+a 键盘事件上调用函数
Posted
技术标签:
【中文标题】Python/Kivy:如何在 ctrl+a 键盘事件上调用函数【英文标题】:Python/Kivy : How to call function on ctrl+a keyboard events 【发布时间】:2018-09-18 11:43:06 【问题描述】:我正在使用 python-2.7
和 kivy-1.9.0
。当我运行 test.py 时,会显示 Menu1
,Menu2
菜单。
当我点击 Menu1
然后显示 +Add Menu1
文本。当我点击 +Add Menu1
然后调用函数 root.add_menu1()
。如何使用ctrl+a
键盘键事件而不是鼠标单击来实现它?
同样,如果我点击Menu2
然后显示+Add Menu2
文本。之后我按ctrl+a
键然后应该调用root.add_menu2()
function。
test.py
import kivy
kivy.require('1.9.0')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty, ListProperty, ObjectProperty,NumericProperty
from kivy.uix.dropdown import DropDown
from kivy.core.window import Window
Window.maximize()
class MENU1(BoxLayout):
def add_menu1(self):
pass
class MENU2(BoxLayout):
def add_menu2(self):
pass
class MainMenu(BoxLayout):
test1_or_test2 = ObjectProperty()
def display_menu1(self):
self.remove_widgets()
self.menu1 = MENU1()
self.test1_or_test2.add_widget(self.menu1)
def display_menu2(self):
self.remove_widgets()
self.menu2 = MENU2()
self.test1_or_test2.add_widget(self.menu2)
def remove_widgets(self):
self.test1_or_test2.clear_widgets()
class Test(App):
title = "test"
def build(self):
return MainMenu()
if __name__ == '__main__':
Test().run()
test.kv
<MENU1>:
BoxLayout:
orientation: "vertical"
Button:
size_hint: .06, .03
text: "+Add Menu1"
on_press: root.add_menu1()
GridLayout:
size_hint: 1, None
size_hint_y:None
height: 25
cols: 5
BoxLayout:
Label:
text:""
<MENU2>:
BoxLayout:
orientation: "vertical"
Button:
size_hint: .06, .03
text: "+Add Menu2"
on_press: root.add_menu2()
GridLayout:
size_hint: 1, None
size_hint_y:None
height: 25
cols: 5
BoxLayout:
Label:
text:""
<MenuButton@Button>:
text_size: self.size
valign: "middle"
padding_x: 5
size : (80,30)
size_hint : (None, None)
background_color: 90 , 90, 90, 90
background_normal: ''
color: 0, 0.517, 0.705, 1
border: (0, 10, 0, 0)
<MainMenu>:
test1_or_test2: test1_or_test2
BoxLayout:
orientation: 'vertical'
BoxLayout:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
size_hint_y: 1
MenuButton:
id: btn
text: 'Menu1'
size : (60,30)
on_release: root.display_menu1()
MenuButton:
id: btn
text: 'Menu2'
size : (60,30)
on_release: root.display_menu2()
BoxLayout:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
Color:
rgb: (1,1,1)
Label:
size_hint_x: 45
BoxLayout:
id: test1_or_test2
size_hint_y: 10
Label:
size_hint_y: 1
【问题讨论】:
查看***.com/questions/17280341/… 可能会有帮助! @fins 感谢您的帮助。它正在工作 【参考方案1】:-
将两种方法 display_menu1 和 display_menu2 组合成一个通用方法 display_menu。
将方法 add_menu1 和 add_menu2 重命名为 add_menu。
添加了一个新的 ObjectProperty、menu 以连接到 Menu1 或 Menu2。
使用按键绑定,检查ctrl+a 以调用add_menu。
示例
main.py
>from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty, ListProperty, ObjectProperty, NumericProperty, StringProperty
from kivy.uix.dropdown import DropDown
from kivy.core.window import Window
Window.maximize()
class MENU1(BoxLayout):
def add_menu(self):
print("MENU1.add_menu")
class MENU2(BoxLayout):
def add_menu(self):
print("MENU2.add_menu")
class MainMenu(BoxLayout):
test1_or_test2 = ObjectProperty(None)
menu = ObjectProperty(None)
def __init__(self, **kwargs):
super(MainMenu, self).__init__(**kwargs)
Window.bind(on_key_down=self._on_keyboard_down)
def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
if len(modifiers) > 0 and modifiers[0] == 'ctrl' and text == 'a': # Ctrl+a
print("\nThe key", keycode, "have been pressed")
print(" - text is %r" % text)
print(" - modifiers are %r" % modifiers)
self.menu.add_menu()
def display_menu(self, instance):
self.remove_widgets()
if instance.text == 'Menu1':
self.menu = MENU1()
elif instance.text == 'Menu2':
self.menu = MENU2()
self.test1_or_test2.add_widget(self.menu)
def remove_widgets(self):
self.test1_or_test2.clear_widgets()
class Test(App):
title = "test"
def build(self):
return MainMenu()
if __name__ == '__main__':
Test().run()
test.kv
#:kivy 1.10.0
<MENU1>:
BoxLayout:
orientation: "vertical"
Button:
size_hint: .06, .03
text: "+Add Menu1"
on_press: root.add_menu()
GridLayout:
size_hint: 1, None
size_hint_y:None
height: 25
cols: 5
BoxLayout:
Label:
text:""
<MENU2>:
BoxLayout:
orientation: "vertical"
Button:
size_hint: .06, .03
text: "+Add Menu2"
on_press: root.add_menu()
GridLayout:
size_hint: 1, None
size_hint_y:None
height: 25
cols: 5
BoxLayout:
Label:
text:""
<MenuButton@Button>:
text_size: self.size
valign: "middle"
padding_x: 5
size : (80,30)
size_hint : (None, None)
background_color: 90 , 90, 90, 90
background_normal: ''
color: 0, 0.517, 0.705, 1
border: (0, 10, 0, 0)
<MainMenu>:
test1_or_test2: test1_or_test2
BoxLayout:
orientation: 'vertical'
BoxLayout:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
size_hint_y: 1
MenuButton:
id: btn
text: 'Menu1'
size : (60,30)
on_release: root.display_menu(self)
MenuButton:
id: btn
text: 'Menu2'
size : (60,30)
on_release: root.display_menu(self)
BoxLayout:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
Color:
rgb: (1,1,1)
Label:
size_hint_x: 45
BoxLayout:
id: test1_or_test2
size_hint_y: 10
Label:
size_hint_y: 1
输出
【讨论】:
以上是关于Python/Kivy:如何在 ctrl+a 键盘事件上调用函数的主要内容,如果未能解决你的问题,请参考以下文章