繁体   English   中英

如何在 kivy 中更新 Widget 的 on_touch_move 和 on_touch_down 方法

[英]How to update the on_touch_move and on_touch_down methods of a Widget in kivy

我目前正在创建一个绘画应用程序,为此我正在使用一个 Widget 画布。 基本上我已经定义了创建直线和曲线的方法。 但问题是我无法在这两种方法之间切换。 这意味着我有两个按钮来触发这些方法,每个方法都使用on_touch_downon_touch_move方法绑定小部件。 我的问题是第一次应用程序开始运行时,例如我点击了名称为f hand按钮,它工作正常,之后,一旦我点击下一个按钮,小部件就会与这两种方法绑定在一起,造成一团糟......现在我希望小部件一次只用一种方法绑定自己..我怎么能做到这一点..

我的python代码在这里

import kivy
from kivy.uix.widget import Widget
from kivy.uix.widget import Canvas
from kivy.graphics import Color
from kivy.graphics import Line
from kivy.uix.floatlayout import FloatLayout
from kivy.app import App
from kivy.uix.button import Button

class Main(FloatLayout):


    def __init__(self,**kwargs):
        super(Main, self).__init__(**kwargs)

        self.my_widget = Widget(size_hint= (0.6,0.6),pos_hint = {'x':0.5,'top' : 0.8})
        self.add_widget(self.my_widget)

        self.free_hand = Button(text='f_hand',pos_hint = {'x':0.04,'top':0.2},size_hint = (0.12,0.12))
        self.add_widget(self.free_hand)
        self.free_hand.bind(on_press = self._free_hand)

        self._hand = Button(text='s_hand', pos_hint={'x': 0.04, 'top': 0.6}, size_hint=(0.12, 0.12))
        self.add_widget(self._hand)
        self._hand.bind(on_press=self._straight_lines)


    def _free_hand(self,instance):


        def on_touch_downah(self,touch):
            self.line = Line(points = (touch.x,touch.y),width = 5)
            self.canvas.add(self.line)
            '''self.line_list.append(self.line)
            print(self.line_list)'''
        
        def on_touch_moveah(self,touch):
            self.line.points += touch.x,touch.y

        self.my_widget.bind(on_touch_down=on_touch_downah)
        self.my_widget.bind(on_touch_move=on_touch_moveah)

    def _straight_lines(self,instance):

        def on_touch_downeh(self, touch):
            self.x_ = touch.x
            self.y_ = touch.y
            self.lines = Line(points=(touch.x,touch.y),width = 5)
            self.canvas.add(self.lines)

        def on_touch_moveeh(self, touch):
            self.x2 = self.x_ + 0.1
            self.y2 = self.y_ + 0.1
            self.lines.points = (self.x_,self.y_,self.x2,self.y2,touch.x,touch.y)

        self.my_widget.bind(on_touch_down=on_touch_downeh)
        self.my_widget.bind(on_touch_move=on_touch_moveeh)


'''
    def undo_1(self):
        self.i -= 1
        k = self.line_list[self.i]
        self.canvas.remove(k)
'''

class Myapp(App):
    def build(self):
        return Main()

Myapp().run()

任何帮助是极大的赞赏 。

扩展来自@inclement 的建议,这是您的代码的修改版本,其中只有两个方法绑定到on_touch_downon_touch_move Buttons 触发的方法只是设置绘制mode ,绑定方法根据当前模式选择要做什么:

from kivy.uix.widget import Widget
from kivy.graphics import Line
from kivy.uix.floatlayout import FloatLayout
from kivy.app import App
from kivy.uix.button import Button

class Main(FloatLayout):


    def __init__(self,**kwargs):
        super(Main, self).__init__(**kwargs)

        self.my_widget = Widget(size_hint= (0.6,0.6),pos_hint = {'x':0.5,'top' : 0.8})
        self.add_widget(self.my_widget)

        self.free_hand = Button(text='f_hand',pos_hint = {'x':0.04,'top':0.2},size_hint = (0.12,0.12))
        self.add_widget(self.free_hand)
        self.free_hand.bind(on_press = self._free_hand)

        self._hand = Button(text='s_hand', pos_hint={'x': 0.04, 'top': 0.6}, size_hint=(0.12, 0.12))
        self.add_widget(self._hand)
        self._hand.bind(on_press=self._straight_lines)

        self.mode = None  # could be "free_hand" or "straight_lines"
        self.my_widget.bind(on_touch_down=self.on_touch_downh)
        self.my_widget.bind(on_touch_move=self.on_touch_moveh)


    def _free_hand(self,instance):
        self.mode = "free_hand"

    def _straight_lines(self,instance):
        self.mode = "straight_lines"

    def on_touch_downh(self, widget, touch):
        if self.mode == "free_hand":
            self.free_hand_touch(touch)
        elif self.mode == "straight_lines":
            self.straight_lines_touch(touch)

    def on_touch_moveh(self, widget, touch):
        if self.mode == "free_hand":
            self.free_hand_move(touch)
        elif self.mode == "straight_lines":
            self.straight_lines_move(touch)

    def free_hand_touch(self,touch):
        self.line = Line(points = (touch.x,touch.y),width = 5)
        self.canvas.add(self.line)
        '''self.line_list.append(self.line)
        print(self.line_list)'''

    def free_hand_move(self,touch):
        self.line.points += touch.x,touch.y

    def straight_lines_touch(self, touch):
        self.x_ = touch.x
        self.y_ = touch.y
        self.lines = Line(points=(touch.x,touch.y),width = 5)
        self.canvas.add(self.lines)

    def straight_lines_move(self, touch):
        self.x2 = self.x_ + 0.1
        self.y2 = self.y_ + 0.1
        self.lines.points = (self.x_,self.y_,self.x2,self.y2,touch.x,touch.y)

'''
    def undo_1(self):
        self.i -= 1
        k = self.line_list[self.i]
        self.canvas.remove(k)
'''

class Myapp(App):
    def build(self):
        return Main()

Myapp().run()

请注意,在您的原始代码中,每次按下按钮之一时,您都会绑定更多方法。

暂无
暂无

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

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