首页 > 解决方案 > 如何在我的应用程序中以 kv lang 或普通 kivy 向屏幕添加签名/绘画方法?

问题描述

如何在没有 on_touch_down/move 干扰的情况下将按钮添加到我paintScreen的班级并使其在BoxLayout画布内绘制并将按钮分隔在不同的框中,以便它不会在按钮上绘制?这是我目前遇到的问题。此外,如果该问题得到解决,那么如果我使用该图,我是否能够将该图保存为 imgexport_to_png

from kivy.app import App
from kivy.graphics.context_instructions import Color
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.spinner import Spinner
from kivy.uix.checkbox import CheckBox
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.scrollview import ScrollView
from kivy.graphics import Line, Rectangle, Ellipse, Triangle

Builder.load_string("""
<firstScreen>
BoxLayout:
    orientation: "vertical"
    
    ScrollView:
        size: root.size
        GridLayout:
            orientation: "vertical"
            size_hint_y: None
            height: self.minimum_height  #<<<<<<<<<<<<<<<<<<<<
            row_default_height: 90
            cols: 2
    
            Label:
                text: "Name: "
            TextInput:
                multiline: False

            Label:
                text: "Address: "
            TextInput:
                multiline: False
                
            Label:
                text: "Contact: "
            TextInput:
                multiline: False

            Label:
                text: "Telephone: "
            TextInput:
                multiline: False

            Label:
                text: "Order Number: "
            TextInput:
                multiline: False

            Label:
                text: "Transporter's Name: "
            TextInput:
                multiline: False

            Label:
                text: "Vehicle Reg. No.: "
            TextInput:
                multiline: False

            Label:
                text: "Driver's Name: "
            TextInput:
                multiline: False

            Label:
                text:"Hazchem Requirements: "

            Spinner:
                id: spinner_1
                text: "< Select >"
                values: root.pick_opt

            Spinner:
                id: spinner_2
                text: "Waste Details"
                values: root.pick_classification
            
            GridLayout:
                size_hint: 1, None
                rows:2

                Label:
                    text: "Non-Hazardous"

                Label:
                    text: "Hazardous"

                CheckBox:
                    on_active: root.chk_tick

                CheckBox:
                    on_active: root.chk_tick
            
                    
            Spinner:
                id: spinner_3
                text: "Service Details"
                values: root.sog
                
            TextInput:
                multiline: False
                hint_text: "Enter Volume/Weight"
                
            Spinner:
                id: spinner_4
                text: "Service Details"
                values: root.sog
                
            TextInput:
                multiline: False
                hint_text: "Enter Volume/Weight"
                
            Spinner:
                id: spinner_5
                text: "Service Details"
                values: root.sog
                
            TextInput:
                multiline: False
                hint_text: "Enter Volume/Weight"
            
            Spinner:
                id: spinner_6
                text: "Service Details"
                values: root.sog
                
            TextInput:
                multiline: False
                hint_text: "Enter Volume/Weight"


    Button:
        text: "Next Screen"
        size_hint: 1,.1
        on_press: root.manager.current = "screen_2"


<secondScreen>
BoxLayout:
    orientation: "horizontal"
    
    ScrollView:
        size: root.size
        GridLayout:
            orientation: "vertical"
            size_hint_y: None
            height: self.minimum_height  #<<<<<<<<<<<<<<<<<<<<
            row_default_height: 120
            cols: 1
    
            BoxLayout:
                orientation: 'vertical'
                Spinner:
                    id: spinner_7
                    text: "details"
                    width: self.parent.width * 2
                    values: root.pick_dets
            
                
                Spinner:
                    id: spinner_8
                    text: "details"
                    values: root.pick_dets
                    
                Spinner:
                    id: spinner_9
                    text: "details"
                    values: root.pick_dets
                    
        
            GridLayout:
                cols: 2
                rows: 3
        
                Label:
                    text: "Start Time"
        
                Label:
                    text: "End Time"
        
                TextInput:
                    multiline: False
        
                TextInput:
                    multiline: False
            GridLayout:
                cols: 2        
                Label: 
                    text: 'Special Instructions/Remarks'
                    
                TextInput:
                    hint_text: 'Enter Remarks'
                    
                    
            Button:
                id: signature
                text: 'Signature'
                on_press: root.manager.current = "screen_signature"
            
            BoxLayout:
                orientation: 'vertical'
                        
                Button:
                    text: "Previous Screen"
                    size_hint: 1,.1
                    on_press: root.manager.current = "screen_1"
                    
                Button:
                    text: "Next Screen"
                    size_hint: 1,.1
                    on_press: root.manager.current = "screen_2"

<paintScreen>

""")

class paintScreen(Screen):

# On mouse press how Paint_brush behave
def on_touch_down(self, touch):
    color = (1, 1, 1)
    with self.canvas:
        Color(*color, mode='hsv')
        d = 30.
        Line(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
        touch.ud['line'] = Line(points=(touch.x, touch.y))

    # On mouse movement how Paint_brush behave

def on_touch_move(self, touch):
    touch.ud['line'].points += [touch.x, touch.y]

pass

class firstScreen(Screen):
def __init__(self, **kwargs):
    self.pick_opt = ["yes", "no"]
    self.pick_classification = ["Classification (SANAS 10228)", "HR (1,2,3,4 - Minimum                                                          Requirements)",
                                "Emergency Response Guide Reference"]
    self.sog = ['Oil', "Effluent", "Sludge", "Other"]

    super(firstScreen, self).__init__(**kwargs)

def chk_tick(self, instance, value):
    if value:
        print("ticked")
    else:
        print('unticked')

pass


class secondScreen(Screen):
def __init__(self, **kwargs):
    self.pick_dets = ["HP Cleaning", "Chemicals", "Soil Treatment",
                      "Minor Civils & Plumbing", "Effluent Treatment", "Other"]

    super(secondScreen, self).__init__(**kwargs)

pass


class MyScreenManager(ScreenManager, RecycleView):
pass


screen_manager: ScreenManager = ScreenManager()
screen_manager.add_widget(firstScreen(name="screen_1"))
screen_manager.add_widget(secondScreen(name="screen_2"))
screen_manager.add_widget(paintScreen(name="screen_signature"))


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

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


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

标签: pythonkivykivy-language

解决方案


推荐阅读