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

Python 如何在kivy中更新Widget的on_touch_move和on_touch_down方法,python,canvas,kivy,bind,Python,Canvas,Kivy,Bind,我目前正在创建一个绘画应用程序,我正在为此使用一个小部件画布。 基本上,我已经定义了创建直线和曲线的方法。但问题是我无法在这两种方法之间切换。这意味着我有两个按钮来触发这些方法,每个方法都用on\u touch\u down和on\u touch\u move方法绑定小部件。我的问题是,当应用程序第一次开始运行时,例如,我单击了名为f hand的按钮,它工作正常,然后单击了下一个按钮,小部件就会用这两种方法绑定自己,造成混乱。。。。现在我希望小部件一次只能绑定一个方法。。我怎样才能做到这一点 我

我目前正在创建一个绘画应用程序,我正在为此使用一个小部件画布。 基本上,我已经定义了创建直线和曲线的方法。但问题是我无法在这两种方法之间切换。这意味着我有两个按钮来触发这些方法,每个方法都用
on\u touch\u down
on\u touch\u 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()

非常感谢您的帮助。

根据@Increment的建议,这里是您代码的一个修改版本,其中只有两种方法绑定到按下按钮时的
和移动按钮时的
。按钮触发的方法只是为图形设置
模式
,绑定方法根据当前模式选择要执行的操作:

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()

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

不要将许多不同的函数绑定到on\u touch\u down/on\u touch\u move。而是将一个函数绑定到一个当时做正确事情的函数,即如果self.mode==“free\u hand”:self.do\u free\u hand\u on_touch\u down(),该函数应该执行
;elif self.mode==“straight\u lines”:self.do\u straight\u lines\u on\u touch\u down()…
这只是一个例子,显然你需要填写self.mode并设置你想要调用的方法。基本上我不理解你写的代码。你能详细说明一下吗。我不熟悉kivy和python。谢谢你的快速回复