Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将kivy中的文本输入保存到变量上_Python_Kivy - Fatal编程技术网

Python 如何将kivy中的文本输入保存到变量上

Python 如何将kivy中的文本输入保存到变量上,python,kivy,Python,Kivy,我是kivy的初学者,我正在尝试制作这个应用程序,下面的代码需要一些帮助,帮助我如何将人名保存到变量中,以便在其他屏幕上使用它。 自定义屏幕类有我的“保存名称”按钮和文本输入,我想保存此屏幕的文本输入。当用户按下“保存名称”时,它会将屏幕更改为“驱动屏幕”,并希望在此屏幕中使用该名称 请耐心阅读,如果可以,请帮助我。 多谢各位 from kivy.app import App import cv2 import numpy as np from kivy.uix.label import Lab

我是kivy的初学者,我正在尝试制作这个应用程序,下面的代码需要一些帮助,帮助我如何将人名保存到变量中,以便在其他屏幕上使用它。 自定义屏幕类有我的“保存名称”按钮和文本输入,我想保存此屏幕的文本输入。当用户按下“保存名称”时,它会将屏幕更改为“驱动屏幕”,并希望在此屏幕中使用该名称 请耐心阅读,如果可以,请帮助我。 多谢各位

from kivy.app import App
import cv2
import numpy as np
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.screenmanager import SlideTransition
from kivy.uix.textinput import TextInput
from kivy.properties import StringProperty

class CustomScreen(Screen):


    def softAlarm(self, *args):
        self.sound = SoundLoader.load('Soft-alarm-tone.mp3')
        self.sound.loop = True
        self.sound.play()



    def __init__(self, **kwargs):

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

 
        layout = BoxLayout(orientation='vertical')


        layout.add_widget(Label(text='Driving Patner', font_size=50))


        navig = BoxLayout(size_hint_y=0.2)

        label1 = Label(text='Enter your name:', font_size = 30 , valign = 'bottom')

        navig.add_widget(label1)
      
        next = TextInput(font_size=30)

        name = next.text

        btn=Button(text='Save Name', font_size=30 , valign = 'bottom' ,halign='right')

        btn.bind(on_release=self.switch_next)
 
        navig.add_widget(next)
        navig.add_widget(btn)
        layout.add_widget(navig)


        self.add_widget(layout)


#Here we can write the code to start video analysis and all that
    def switch_next(self, *args):
        self.manager.transition = SlideTransition(direction="right")
        self.manager.current = self.manager.next()



class DrivingScreen(Screen):



    def __init__(self, **kwargs):

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




        layout = BoxLayout(orientation='vertical')


        layout.add_widget(Label(text='Safe Driving', font_size=50))


        navig = BoxLayout(size_hint_y=0.2)

        next = Button(text='Start Driving')

        next.bind(on_release=self.switch_next)

        navig.add_widget(next)
        layout.add_widget(navig)

        self.add_widget(layout)

#Here we can write to stop video analysis 
    def switch_next(self, *args):
        self.manager.transition = SlideTransition(direction="right")
        self.manager.current = self.manager.next()
        cap = cv2.VideoCapture(0)
        face = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') #reference: https://github.com/opencv/opencv/tree/master/data/haarcascades
        eyes = cv2.CascadeClassifier('haarcascade_eye.xml') #reference: https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_eye.xml
        while (True):

                ret, frame = cap.read()

                gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                faces = face.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)

                if len(faces) > 0:
                        print('Face Detected')
                else:
                        print('Face not Detected')
                #Clock.schedule_once(self.softAlarm, 3)
                #time.sleep(5) this freezes the whole program, best to find another way to implement
                #Event.wait(5)
                        cv2.rectangle(frame,(0,200), (640,300),(0,255,0),cv2.FILLED)
                        cv2.putText(frame, "User Distracted",(150,265),cv2.FONT_HERSHEY_COMPLEX_SMALL,2,(0,0,0),2 )
                #softAlarm()

                for (x, y, w, h) in faces:
                #getting coordinates of face
                #print(x,y,w,h)
                        roi = frame[y:y+h, x:x+w]

                #setting up bounding boxes for face
                        color = (0, 0, 255)                     #note that colors are in BGR and not RGB
                        color2 = (0, 255, 0 )
                        stroke = 3
                        width = x+w
                        height = y+h

                #drawing the bounding box with text
                        cv2.rectangle(frame, (x, y), (width, height), color, stroke )
                        cv2.putText(frame, "Face Detected", (x,y-5), cv2.FONT_HERSHEY_PLAIN, 1, color2, 2 )

                        roi_gray = gray[y:y+h, x:x+w]
                        roi_color = frame[y:y+h, x:x+w]
     
                        eye = eyes.detectMultiScale(roi_gray, scaleFactor=1.3, minNeighbors=2)
                        if len(eye) > 0:
                                print('Eyes detected')
                        else:
                                print('Eyes not detected')
                                cv2.rectangle(frame,(0,200), (640,300),(0,0,255),cv2.FILLED)
                                cv2.putText(frame, "Pay Attention!",(150,265),cv2.FONT_HERSHEY_COMPLEX_SMALL,2,(0,0,0),2 )
                        for (ex, ey, ew, eh) in eye:
                                cv2.rectangle(roi_color,(ex,ey), (ex+ew, ey+eh),color2,2)
                cv2.imshow('Video', frame)

                if cv2.waitKey(1) & 0xFF == ord('q'):
                        break
        cap.release()
        cv2.destroyAllWindows()

class DrivingSummary(Screen):



    def __init__(self, **kwargs):

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




        layout = BoxLayout(orientation='vertical')


        layout.add_widget(Label(text='Here is your driving summary', font_size=50))


        navig = BoxLayout(size_hint_y=0.2)

        next = Button(text='START DRIVING AGAIN')

        next.bind(on_release=self.switch_next)

        navig.add_widget(next)
        layout.add_widget(navig)

        self.add_widget(layout)


    def switch_next(self, *args):
        self.manager.transition = SlideTransition(direction="right")
        self.manager.current = self.manager.next()


class ScreenManagerApp(App):


    def build(self):

        root = ScreenManager()
        root.add_widget(CustomScreen(name='Screen1'))
        root.add_widget(DrivingScreen(name='Screen2'))
        root.add_widget(DrivingSummary(name='Screen3'))
        return root


# This is only a protection, so that if the file
# is imported it won't try to launch another App

if __name__ == '__main__':
    # And run the App with its method 'run'
    ScreenManagerApp().run()


您可以保存对
屏幕中小部件的引用。例如,您的姓名
TextInput
可以通过修改
自定义屏幕的
\uuuu init\uuuu()方法来保存:

    ...

    self.next = TextInput(font_size=30)  # save reference to this TextInput

    name = self.next.text

    btn = Button(text='Save Name', font_size=30, valign='bottom', halign='right')

    btn.bind(on_release=self.switch_next)

    navig.add_widget(self.next)

    ...
def switch_next(self, *args):
    self.manager.transition = SlideTransition(direction="right")
    self.manager.current = self.manager.next()
    self.manager.current_screen.driver_name.text = self.next.text
驱动屏幕
方法中,您可以创建一个
标签
来显示名称:

    ...

    self.driver_name = Label(font_size=25)

    layout.add_widget(self.driver_name)

    ...
然后,这些引用可用于
自定义屏幕的
开关\u next()
方法中:

    ...

    self.next = TextInput(font_size=30)  # save reference to this TextInput

    name = self.next.text

    btn = Button(text='Save Name', font_size=30, valign='bottom', halign='right')

    btn.bind(on_release=self.switch_next)

    navig.add_widget(self.next)

    ...
def switch_next(self, *args):
    self.manager.transition = SlideTransition(direction="right")
    self.manager.current = self.manager.next()
    self.manager.current_screen.driver_name.text = self.next.text
请您提供一个程序的最小工作示例,少于100行代码,例如,
opencv
库根本不需要回答您的问题