gpt4 book ai didi

python - 在Kivy中获取IMAGE WIDGET的像素位置

转载 作者:行者123 更新时间:2023-12-01 04:28:36 24 4
gpt4 key购买 nike

下面粘贴的是获取像素位置的代码。但是,我打算在单击鼠标时仅获取图像像素位置。在当前代码中,它在整个窗口上运行。

__version__ = '1.0'

import kivy
kivy.require('1.0.6')

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.graphics import Color, Rectangle, Point, GraphicException
from random import random



class Touchtracer(FloatLayout):

def on_touch_down(self, touch):
win = self.get_parent_window()
ud = touch.ud
ud['group'] = g = str(touch.uid)
pointsize = 5
if 'pressure' in touch.profile:
ud['pressure'] = touch.pressure
pointsize = (touch.pressure * 100000) ** 2
ud['color'] = random()

with self.canvas:
Color(ud['color'], 1, 1, mode='hsv', group=g)
ud['lines'] = [
Rectangle(pos=(touch.x, 0), size=(1, win.height), group=g),
Rectangle(pos=(0, touch.y), size=(win.width, 1), group=g),
Point(points=(touch.x, touch.y), source='particle.png',
pointsize=pointsize, group=g)]

ud['label'] = Label(size_hint=(None, None))
self.update_touch_label(ud['label'], touch)
self.add_widget(ud['label'])
touch.grab(self)
return True



def on_touch_up(self, touch):
if touch.grab_current is not self:
return
touch.ungrab(self)
ud = touch.ud
self.canvas.remove_group(ud['group'])
self.remove_widget(ud['label'])

def update_touch_label(self, label, touch):
label.text = 'ID: %s\nPos: (%d, %d)\nClass: %s' % (
touch.id, touch.x, touch.y, touch.__class__.__name__)
label.texture_update()
label.pos = touch.pos
label.size = label.texture_size[0] + 20, label.texture_size[1] + 20


class TouchtracerApp(App):


def build(self):
return Touchtracer()

def on_pause(self):
return True

if __name__ == '__main__':
TouchtracerApp().run()

touchtracer.kv 文件:

#:kivy 1.0
#:import kivy kivy

<Touchtracer>:
img:img
Image:
id: img

source: 'image.jpg'

size: 200,200
BoxLayout:
padding: '10dp'
spacing: '10dp'
size_hint: 1, None
pos_hint: {'top': 1}
height: '44dp'
Image:
size_hint: None, None
size: '24dp', '24dp'
source: 'data/logo/kivy-icon-64.png'
mipmap: True
Label:
height: '24dp'
text_size: self.width, None
color: (1, 1, 1, .8)
text: 'Kivy %s - Touchtracer' % kivy.__version__
valign: 'middle'

如何使其仅适用于图像?

最佳答案

您只想在计算之前测试与图像小部件的碰撞?如果是这样,您可以执行以下操作:

def on_touch_down(self, touch):
if not self.img.collide_point(*touch.pos):
return False
# Put the rest of your code here, just like it already is

关于python - 在Kivy中获取IMAGE WIDGET的像素位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32740718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com