Skip to main content
 首页 » 编程设计

python之Kivy:处理事件

2024年10月01日9oomusou

Palete 类中是按钮,它与Paint 类中删除 Canvas 内容的方法绑定(bind)。我认为,解决是使用自定义事件。我对事件没有经验,也没有理解 kivy 手册中的描述。我需要保留类的结构。你能帮帮我吗?

from kivy.app import App 
from kivy.uix.widget import Widget 
 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.button import Button 
 
from kivy.graphics import Color, Rectangle 
from functools import partial 
 
 
class Paint(Widget): 
 
    def __init__(self, palete,**kwargs): 
        # make sure we aren't overriding any important functionality 
        super(Paint, self).__init__(**kwargs) 
        self.palete = palete 
        self.contents = [] 
 
    def on_touch_down(self, touch): 
        color = self.palete.act_col 
        with self.canvas: 
            Color(color[0], color[1], color[2]) 
            sqr = Rectangle(pos = (touch.x, touch.y), size=(20, 40)) 
            self.contents.append(sqr) 
 
class Palete(BoxLayout): 
    def __init__(self, **kwargs): 
        super(Palete, self).__init__(**kwargs) 
 
        self.act_col= (1, 0, 0) 
 
        self.licol =[] 
        self.licol.append((1, 0, 0)) 
        self.licol.append((0, 1, 0)) 
 
        self.layout = BoxLayout(size_hint=(1, None), height=50) 
 
        for i in range(0, len(self.licol)): 
            but = Button( id = str(i)) 
            col = self.licol[i] 
            but.background_normal = '' 
            but.background_color = (col[0], col[1], col[2], 1) 
 
 
            act = partial(self.SetColor, but.id) 
            but.bind(on_press=act) 
            self. layout.add_widget(but) 
 
        but = Button(text="<--",on_press = self.ClearContents) 
        self.layout.add_widget(but) 
 
    def SetColor(self,_col, h): 
        ind = int(_col) 
        self.act_col = self.licol[ind] 
        pass 
 
    def ClearContents(self, obj): 
        if len(self.contents)!= 0: 
            self.canvas.remove(self.contents[-1]) 
            self.contents = self.contents[:-1] 
 
 
class MyPaintApp(App): 
 
    def build(self): 
        root = BoxLayout(orientation='vertical') 
        self.palete = Palete() 
 
        self.paint =Paint(self.palete) 
        root.add_widget(self.paint) 
 
        root.add_widget(self.palete.layout) 
 
        return root 
 
 
if __name__ == '__main__': 
    MyPaintApp().run() 

请您参考如下方法:

canvas 和contents 是 Paint 类的属性,您正试图从 Palate 访问它类。

如果不想改变结构,可以将 Paint 类的引用传递给 Palete 类:

from kivy.app import App 
from kivy.uix.widget import Widget 
 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.button import Button 
 
from kivy.graphics import Color, Rectangle 
from functools import partial 
 
 
class Paint(Widget): 
 
    def __init__(self, palete,**kwargs): 
        # make sure we aren't overriding any important functionality 
        super(Paint, self).__init__(**kwargs) 
        self.palete = palete 
        self.palete.paint = self 
        self.contents = [] 
 
 
    def on_touch_down(self, touch): 
        color = self.palete.act_col 
        with self.canvas: 
            Color(color[0], color[1], color[2]) 
            sqr = Rectangle(pos = (touch.x, touch.y), size=(20, 40)) 
            self.contents.append(sqr) 
 
class Palete(BoxLayout): 
    def __init__(self, **kwargs): 
        super(Palete, self).__init__(**kwargs) 
        self.act_col= (1, 0, 0) 
        self.licol =[] 
        self.licol.append((1, 0, 0)) 
        self.licol.append((0, 1, 0)) 
        self.paint = None 
 
        self.layout = BoxLayout(size_hint=(1, None), height=50) 
 
        for i in range(0, len(self.licol)): 
            but = Button( id = str(i)) 
            col = self.licol[i] 
            but.background_normal = '' 
            but.background_color = (col[0], col[1], col[2], 1) 
 
 
            act = partial(self.SetColor, but.id) 
            but.bind(on_press=act) 
            self. layout.add_widget(but) 
 
        but = Button(text="<--",on_press = self.ClearContents) 
        self.layout.add_widget(but) 
 
    def SetColor(self,_col, h): 
        ind = int(_col) 
        self.act_col = self.licol[ind] 
        pass 
 
    def ClearContents(self, obj): 
        if not self.paint: 
            return 
        if self.paint.contents: 
            self.paint.canvas.remove(self.paint.contents.pop()) 
 
 
class MyPaintApp(App): 
 
    def build(self): 
        root = BoxLayout(orientation='vertical') 
        self.palete = Palete() 
 
        self.paint = Paint(self.palete) 
        root.add_widget(self.paint) 
 
        root.add_widget(self.palete.layout) 
 
        return root 
 
 
if __name__ == '__main__': 
    MyPaintApp().run() 

我认为您应该从实现 Canvas 的类中删除对象以保留封装。另一种选择是将回调移动到 Paint 类并将 on_press 事件绑定(bind)到它(来自 Paint 类):

from kivy.app import App 
from kivy.uix.widget import Widget 
 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.button import Button 
 
from kivy.graphics import Color, Rectangle 
from functools import partial 
 
 
class Paint(Widget): 
 
    def __init__(self, palete,**kwargs): 
        # make sure we aren't overriding any important functionality 
        super(Paint, self).__init__(**kwargs) 
        self.palete = palete 
        self.contents = [] 
        self.palete.clear_btn.bind(on_press = self.clear_contents) 
 
    def on_touch_down(self, touch): 
        color = self.palete.act_col 
        with self.canvas: 
            Color(color[0], color[1], color[2]) 
            sqr = Rectangle(pos = (touch.x, touch.y), size=(20, 40)) 
            self.contents.append(sqr) 
 
    def clear_contents(self, obj): 
        if self.contents: 
            self.canvas.remove(self.contents.pop()) 
 
class Palete(BoxLayout): 
    def __init__(self, **kwargs): 
        super(Palete, self).__init__(**kwargs) 
        self.act_col= (1, 0, 0) 
        self.licol =[] 
        self.licol.append((1, 0, 0)) 
        self.licol.append((0, 1, 0)) 
        self.paint = None 
 
        self.layout = BoxLayout(size_hint=(1, None), height=50) 
 
        for i in range(0, len(self.licol)): 
            but = Button( id = str(i)) 
            col = self.licol[i] 
            but.background_normal = '' 
            but.background_color = (col[0], col[1], col[2], 1) 
 
 
            act = partial(self.set_color, but.id) 
            but.bind(on_press=act) 
            self. layout.add_widget(but) 
 
        self.clear_btn = Button(text="<--") 
        self.layout.add_widget(self.clear_btn) 
 
    def set_color(self,_col, h): 
        ind = int(_col) 
        self.act_col = self.licol[ind] 
 
 
class MyPaintApp(App): 
 
    def build(self): 
        root = BoxLayout(orientation='vertical') 
        self.palete = Palete() 
 
        self.paint = Paint(self.palete) 
        root.add_widget(self.paint) 
 
        root.add_widget(self.palete.layout) 
 
        return root 
 
 
if __name__ == '__main__': 
    MyPaintApp().run() 

使用 list.pop 而不是切片来从列表中删除最后一项。它更具可读性,并且不会每次都复制列表。