Помощь кнопка меню в коде

Karina. 234567

Новичок
Пользователь
Окт 18, 2020
16
0
1
Помогите пожалуйста у меня есть код что то наподобие рисовалки, как сделать кнопку на которую нажимаешь и можешь выбрать цвет. То есть ни как сейчас они все по отдельности, а в одном меню например"краски"
Код:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button

from random import random
from kivy.core.window import Window
from kivy.graphics import (Color, Ellipse, Rectangle, Line)


class PainterWidget(Widget):
    color = (0.2, 0, 1, 1)

    def on_touch_down(self, touch):
        with self.canvas:
            Color(rgba=self.color)
            touch.ud["line"] = Line(points=(touch.x, touch.y), width=15)

    def on_touch_move(self, touch):
        touch.ud["line"].points += (touch.x, touch.y)
    
class PaintApp(App):
    def build(self):
        parent = Widget()
        self.painter = PainterWidget()
        parent.add_widget(self.painter)

        parent.add_widget(Button(text = 'Clear' , on_press = self.clear_canvas, size = (100, 50)))
        parent.add_widget(Button(text = 'Save' , on_press = self.save, size = (100, 50), pos = (100, 0)))
        parent.add_widget(Button(text = 'Screen', on_press = self.screen, size = (100, 50), pos=(200, 0 )))
        parent.add_widget(Button(text = 'red', on_press = self.red, size = (100, 50), pos=(300, 0 )))
        parent.add_widget(Button(text = 'white', on_press = self.white, size = (100, 50), pos=(400, 0 )))
        parent.add_widget(Button(text = 'blue', on_press = self.blue, size = (100, 50), pos=(500, 0 )))
      
        return parent
  
    
    def red(self, instance):
        self.painter.color = (1, 0, 0.2, 1)

    def blue(self, instance):
        self.painter.color = (0.2, 0, 1, 1)   

    def white(self, instance):
        self.painter.color = (1, 1, 1, 1)
    
    def clear_canvas(self, instance):
        self.painter.canvas.clear()   

    def save(self, instance):
        self.painter.size = (Window.size[0], Window.size[1])
        self.painter.export_to_png('mage.png')

    def screen(self, instance):
        Window.screenshot('screen.png')
        
if __name__ == '__main__':
    PaintApp().run()
 

stud_55

Модератор
Команда форума
Модератор
Апр 3, 2020
1 522
672
113
Вот пример меню с использованием ActionBar`a:
Python:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.graphics import Color, Line
from kivy.uix.boxlayout import BoxLayout


kv_str = Builder.load_string('''
ActionBar:
    ActionView:
        use_separator: True
        ActionPrevious:
            title: 'PaintApp'
            with_previous: False
        ActionButton:
            text: 'Clear'
            on_press: app.clear_canvas()
        ActionGroup:
            text: 'File'
            mode: 'spinner'
            ActionButton:
                text: 'Save'
                on_press: app.save()
            ActionButton:
                text: 'Screen'
                on_press: app.screen()
        ActionGroup:
            text: 'Colors'
            mode: 'spinner'
            ActionButton:
                text: 'Red'
                on_press: app.change_color((1, 0, 0.2, 1))
            ActionButton:
                text: 'Blue'
                on_press: app.change_color((0.2, 0, 1, 1))
            ActionButton:
                text: 'Green'
                on_press: app.change_color((0.2, 1, 0.2, 1))
            ActionButton:
                text: 'Black'
                on_press: app.change_color((0, 0, 0, 1))
''')


class PainterWidget(Widget):
    color = (0.2, 0, 1, 1)

    def on_touch_down(self, touch):
        with self.canvas:
            Color(rgba=self.color)
            touch.ud["line"] = Line(points=(touch.x, touch.y), width=15)

    def on_touch_move(self, touch):
        touch.ud.get("line").points += (touch.x, touch.y)


class PaintApp(App):
    def build(self):
        parent = BoxLayout(orientation='vertical')
        self.painter = PainterWidget()
        parent.add_widget(self.painter)
        parent.add_widget(kv_str)

        return parent

    def change_color(self, color):
        self.painter.color = color

    def clear_canvas(self):
        self.painter.canvas.clear()

    def save(self):
        self.painter.size = (Window.size[0], Window.size[1])
        self.painter.export_to_png("mage.png")

    def screen(self):
        Window.screenshot("screen.png")


if __name__ == "__main__":
    PaintApp().run()
 

Karina. 234567

Новичок
Пользователь
Окт 18, 2020
16
0
1
Спасибо большое!!!
 

Форум IT Специалистов