python - 如何在kivy中跟踪鼠标事件并为网格着色
问题描述
如何绘制网格并跟踪鼠标,我将在其中使用 Kivy 更改鼠标悬停的框的颜色?
目前我有下面的代码,它还没有完成,但有一些问题。以目前的方式,它只是跟踪鼠标的位置,而不绘制任何网格。如果我只返回布局,则将绘制网格但不会跟踪鼠标。
import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
class TouchInput(Widget):
def on_touch_down(self, touch):
print(touch)
def on_touch_move(self, touch):
print(touch) ## WILL ADD CHANGING COLORS HERE LATER
def on_touch_up(self, touch):
print("RELEASED!",touch)
class MyApp(App):
def build(self):
T = TouchInput()
layout = GridLayout(cols=2)
layout.add_widget(Button(text='Hello 1'))
layout.add_widget(Button(text='World 1'))
layout.add_widget(Button(text='Hello 2'))
layout.add_widget(Button(text='World 2'))
return T
if __name__ == "__main__":
MyApp().run()
解决方案
您可以将您的TouchInput
方法和GridLayout
as 结合起来:
import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
class MyGrid(GridLayout):
def on_touch_down(self, touch):
print(touch)
def on_touch_move(self, touch):
print(touch) ## WILL ADD CHANGING COLORS HERE LATER
def on_touch_up(self, touch):
print("RELEASED!",touch)
class MyApp(App):
def build(self):
layout = MyGrid(cols=2)
layout.add_widget(Button(text='Hello 1'))
layout.add_widget(Button(text='World 1'))
layout.add_widget(Button(text='Hello 2'))
layout.add_widget(Button(text='World 2'))
return layout
if __name__ == "__main__":
MyApp().run()
推荐阅读
- wordpress - 如何使用 Ionic 和 WP-API 更新 JSON
- sql - 基本 Web 开发问题(建立一个有效的测试站点)
- sql - 月份日历查询
- elasticsearch - 在代码中生成查询正文
- angular - 在 Angular 4 中将标头添加到 http.get 时出错
- ios - 与自定义类而不是 SLComposeServiceViewController 共享扩展
- python - 使用opencv区分损坏的帧和视频结尾
- javascript - Selenium (Java):从禁用的输入文本字段中检索值
- c++ - fprintf 在特定字符串上失败,而 shell 函数使用它没有崩溃
- angularjs - 引导程序在角度不起作用