Python 绝望的双击
Python kivy double tap
我正在 Python kivy 中制作一个应用程序,我有一个可拖动的图像。每当用户双击可拖动图像时,它都会打印“这是双击”。
main.py
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.clock import Clock
from functools import partial
from kivy.lang import Builder
from kivy.uix.behaviors import DragBehavior
from kivy.uix.image import Image
class DragImage(DragBehavior, Image):
def on_touch_up(self, touch):
uid = self._get_uid()
if uid in touch.ud:
return super(DragImage, self).on_touch_up(touch)
def on_touch_down(self, touch):
if self.collide_point(*touch.pos) and touch.is_double_tap:
print('this is a double tap')
return super(DragImage, self).on_touch_down(touch)
class TacScreen(Screen):
pass
class MainApp(App):
def build(self):
return Builder.load_string(kv)
MainApp().run()
我认为您可以在图像中使用 on_press。
DragImage
id: tv
pos: 0, 102
size_hint: 1, .1
source: "Tv.png"
on_press: root.tap_count()
现在您可以设置一个变量来计算图像中的点击次数,从零开始。
class DragImage(DragBehavior, Image):
count = 0
def tap_count(self):
count += 1
if count == 2:
count = 0
print("this is a double tap")
def on_touch_up(self, touch):
uid = self._get_uid()
if uid in touch.ud:
return super(DragImage, self).on_touch_up(touch)
def on_touch_down(self, touch):
if self.collide_point(*touch.pos) and touch.is_double_tap:
print('this is a double tap')
return super(DragImage, self).on_touch_down(touch)
我现在无法测试,但理论上它会起作用
如果您将 kv
中的 <DragImage>
规则设置为:
DragImage
id: tv
pos: 0, 102
size_hint: None, .1
width: self.height
source: "Tv.png"
然后 DragImage
将保持方形并且图片将填充 Image
小部件(如果 Tv.png
是方形图片)。
调整 Window
大小时,DragImage
将保持不变 pos
,但不会保持相对于背景图像的相同位置。保持 DragImage
静止的一种方法是使用 pos_hint
,但这不允许拖动。您可以通过将 on_size()
方法添加到 TacScreen
:
来使 DragImage
相对于背景保持静止
class TacScreen(Screen):
old_size = ListProperty([800, 600]) # keeps track of previous size
def on_size(self, screen, new_size):
if self.old_size != new_size:
di = self.ids.tv # get the DragImage
# move the DragImage to keep it stationary relative to background
di.pos = new_size[0] * di.x / self.old_size[0], new_size[1] * di.y / self.old_size[1]
self.old_size = new_size
我正在 Python kivy 中制作一个应用程序,我有一个可拖动的图像。每当用户双击可拖动图像时,它都会打印“这是双击”。
main.py
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.clock import Clock
from functools import partial
from kivy.lang import Builder
from kivy.uix.behaviors import DragBehavior
from kivy.uix.image import Image
class DragImage(DragBehavior, Image):
def on_touch_up(self, touch):
uid = self._get_uid()
if uid in touch.ud:
return super(DragImage, self).on_touch_up(touch)
def on_touch_down(self, touch):
if self.collide_point(*touch.pos) and touch.is_double_tap:
print('this is a double tap')
return super(DragImage, self).on_touch_down(touch)
class TacScreen(Screen):
pass
class MainApp(App):
def build(self):
return Builder.load_string(kv)
MainApp().run()
我认为您可以在图像中使用 on_press。
DragImage
id: tv
pos: 0, 102
size_hint: 1, .1
source: "Tv.png"
on_press: root.tap_count()
现在您可以设置一个变量来计算图像中的点击次数,从零开始。
class DragImage(DragBehavior, Image):
count = 0
def tap_count(self):
count += 1
if count == 2:
count = 0
print("this is a double tap")
def on_touch_up(self, touch):
uid = self._get_uid()
if uid in touch.ud:
return super(DragImage, self).on_touch_up(touch)
def on_touch_down(self, touch):
if self.collide_point(*touch.pos) and touch.is_double_tap:
print('this is a double tap')
return super(DragImage, self).on_touch_down(touch)
我现在无法测试,但理论上它会起作用
如果您将 kv
中的 <DragImage>
规则设置为:
DragImage
id: tv
pos: 0, 102
size_hint: None, .1
width: self.height
source: "Tv.png"
然后 DragImage
将保持方形并且图片将填充 Image
小部件(如果 Tv.png
是方形图片)。
调整 Window
大小时,DragImage
将保持不变 pos
,但不会保持相对于背景图像的相同位置。保持 DragImage
静止的一种方法是使用 pos_hint
,但这不允许拖动。您可以通过将 on_size()
方法添加到 TacScreen
:
DragImage
相对于背景保持静止
class TacScreen(Screen):
old_size = ListProperty([800, 600]) # keeps track of previous size
def on_size(self, screen, new_size):
if self.old_size != new_size:
di = self.ids.tv # get the DragImage
# move the DragImage to keep it stationary relative to background
di.pos = new_size[0] * di.x / self.old_size[0], new_size[1] * di.y / self.old_size[1]
self.old_size = new_size