如何在kivy中的类中插入Python脚本?

如何在kivy中的类中插入Python脚本?,python,kivy,Python,Kivy,我有一个python文本,我想放在kivy的类中。然后我想使用这个类作为函数,并从另一个类调用它。我应该如何定义类?我应该在括号中写什么class FaceGenerator() 如果您想创建一个类,请将对象放在括号中-->类面生成器(对象):但是在您的情况下根本不需要类,您需要的是一个函数。如果我理解正确的话,您只需要在该类上调用一个函数,这样您就可以首先定义一个函数 这里有一种方法可以做我认为你想做的事情: from kivy.app import App from kivy.base im

我有一个python文本,我想放在kivy的类中。然后我想使用这个类作为函数,并从另一个类调用它。我应该如何定义类?我应该在括号中写什么
class FaceGenerator()


如果您想创建一个类,请将
对象
放在括号中-->
类面生成器(对象):
但是在您的情况下根本不需要类,您需要的是一个函数。如果我理解正确的话,您只需要在该类上调用一个函数,这样您就可以首先定义一个函数

这里有一种方法可以做我认为你想做的事情:

from kivy.app import App
from kivy.base import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout


def FaceGenerator():
    #do your stuff
    face = 'PalimPalim'
    return face

Builder.load_string("""
<rootwi>:
    label_to_be_changed: label_to_be_changed
    orientation: 'vertical'
    Button:
        text:'klick me'
        on_press: root.change_Label_text()
    Label:
        id: label_to_be_changed

""")
class rootwi(BoxLayout):
    label_to_be_changed = ObjectProperty()

    def change_Label_text(self):
        temp_str = FaceGenerator()
        self.label_to_be_changed.text = temp_str

class MyApp(App):
    def build(self):
        return rootwi()

if __name__ == '__main__':
    MyApp().run()
从kivy.app导入应用
从kivy.base导入生成器
从kivy.properties导入ObjectProperty
从kivy.uix.boxlayout导入boxlayout
def FaceGenerator():
#做你的事
面='PalimPalim'
回程面
生成器。加载\u字符串(“”)
:
标签更改:标签更改
方向:“垂直”
按钮:
文字:'klick me'
按:root.change\u Label\u text()
标签:
id:要更改的标签
""")
rootwi类(BoxLayout):
label_to_be_change=ObjectProperty()
def更改标签文字(自身):
temp_str=FaceGenerator()
self.label\u to\u be\u change.text=temp\u str
类别MyApp(应用程序):
def生成(自):
返回rootwi()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
MyApp().run()
更多信息

  • 在本例中,kv中的root指最左边的小部件
    rootwi
  • 为作为小部件一部分的小部件定义ObjectProperty是 我最喜欢的更新属性的方法。当然还有其他方法

    • @PalimPalim我在上面的结构中工作过。我的代码现在是这样的 Python代码

      import kivy
      import cv2, os
      import numpy as np
      from PIL import Image
      
      from kivy.app import App
      from kivy.uix.floatlayout import FloatLayout
      from kivy.uix.screenmanager import ScreenManager, Screen
      from kivy.uix.button import Button
      from kivy.uix.widget import Widget
      from kivy.uix.label import Label
      from kivy.lang import Builder
      from kivy.config import Config
      Config.set('graphics', 'fullscreen', '0')
      Config.set('graphics','show_cursor','1')
      
      def FaceGenerator():
      # open the camera and capture video
          cam = cv2.VideoCapture(0)
          face_detector = 
          cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
          ID = 0
          sample_number = 0 # a counter that counts the number of pictures for 
          each person in the database
      
          # detecting the face and draw rectangle on it
          while (True):
              retval,image = cam.read() # reading image from cam
              print np.shape(image)
              gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # converting 
              image to gray image
              faces = face_detector.detectMultiScale(gray_image,1.3,5)
              ''' detectMultiScale, detects objects of different sizes in the 
              input image.
              the detected objects are returned as a list of rectangles
              '''
              for (x,y,w,h) in faces:
                  cv2.rectangle(image, (x,y), (x+w, y+h), (255,0,0), 2)
                  sample_number=sample_number+1
              # saving the captured face in the facebase folder
      
                  cv2.imwrite('Trainer/User.'+str(ID)+'.'+str(sample_number)+'.jpg',
                  gray_image[y:y+h,x:x+w])
          # this loop drawing a rectabgle on the face while the cam is open 
              cv2.imshow('frame',image)
              if cv2.waitKey(100) & 0xFF == ord('q'):
                  break
              elif sample_number==20:
                  break
      
          cam.release()
          cv2.destroyAllWindows()
          output = "Succesfully created trainning set"
          return output
      
      class ScreenOne(Screen):
          pass
      
      class ScreenTwo(Screen):
          pass
      
      class ScreenThree(Screen):
              pass
      
      
      class ScreenManagement(ScreenManager):
          pass
      
      
      sm = Builder.load_file("facerecognition.kv")
      
      class FaceRecognitionApp(App):
          def build(self):
              return sm
      
      if __name__=="__main__":
          FaceRecognitionApp().run()
      
      .KV文件是:

      ScreenManagement:
          id: screen_management
          ScreenOne:
          ScreenTwo:
          ScreenThree:
      
      
      
      <ScreenOne>:
          name: "screen1"
          id: screen_one
      
          FloatLayout:
              canvas.before:
                  Rectangle:
                      source: "image1.jpg"
                      pos:self.pos
                      size:self.size
      
              Label:
                  text:"Hello\n Welcome to my App\n"
                  font_size:40 
                  color: 0,0,0,1
              Button:
                  text: 'Next'
                  font_size: 32 # font size
                  size_hint: .2,.1
                  pos_hint:{'right':1, 'y':0}
                  on_release: app.root.current="screen2"
      
      <ScreenTwo>:
          name: "screen2"
          id: screen_two
      
          FloatLayout:
              canvas:
      
                  Rectangle:
                      source: "image1.jpg"
                      pos:self.pos
                      size:self.size
      
              Label:
                  text:"Please insert your Name\n Please insert your Password\n"
                  font_size:40 
                  color: 0,0,0,1
              Button:
                  text: 'Next'
                  font_size: 32 # font size
                  size_hint: .2,.1
                  pos_hint:{'right':1, 'y':0}
                  on_release: app.root.current="screen3"
      
      <ScreenThree>:
          name: "screen3"
          id: screen_three
      
      
          FloatLayout:
              canvas:
      
                  Rectangle:
                      source: "image1.jpg"
                      pos:self.pos
                      size:self.size
      
      
              Button:
                  text: 'Next'
                  font_size: 32 # font size
                  size_hint: .2,.1
                  pos_hint:{'right':1, 'y':0}
                  on_release: app.root.current="screen1"
      
              BoxLayout:
                  orientation: 'horizontal'
                  FaceGenerator()
      
      屏幕管理:
      id:屏幕管理
      第一屏:
      屏幕二:
      第三幕:
      :
      名称:“屏幕1”
      id:第一屏
      浮动布局:
      在以下情况之前:
      矩形:
      资料来源:“image1.jpg”
      pos:self.pos
      大小:self.size
      标签:
      文本:“您好\n欢迎使用我的应用\n”
      字体大小:40
      颜色:0,0,0,1
      按钮:
      文本:“下一个”
      字体大小:32#字体大小
      大小提示:.2,.1
      位置提示:{'right':1,'y':0}
      发布时:app.root.current=“screen2”
      :
      名称:“屏幕2”
      id:屏幕2
      浮动布局:
      画布:
      矩形:
      资料来源:“image1.jpg”
      pos:self.pos
      大小:self.size
      标签:
      文本:“请插入您的姓名\n请插入您的密码\n”
      字体大小:40
      颜色:0,0,0,1
      按钮:
      文本:“下一个”
      字体大小:32#字体大小
      大小提示:.2,.1
      位置提示:{'right':1,'y':0}
      发布时:app.root.current=“screen3”
      :
      名称:“屏幕3”
      id:第三屏
      浮动布局:
      画布:
      矩形:
      资料来源:“image1.jpg”
      pos:self.pos
      大小:self.size
      按钮:
      文本:“下一个”
      字体大小:32#字体大小
      大小提示:.2,.1
      位置提示:{'right':1,'y':0}
      发布时:app.root.current=“screen1”
      盒子布局:
      方向:“水平”
      面生成器()
      
      Write-in-bracket对象,-->class-FaceGenerator(对象)。但您所定义的并不是一个真正的类。我认为,只要定义一个可以从程序def FaceGenerator()中的任何位置调用的函数,就可以更好地为您服务:我只需要定义一个返回字符串的函数,然后通过查看小部件树并更新标签的文本属性来更新它,而不返回新标签。我认为以下内容只需做一点工作就可以让您的帖子变得更好。实际上,我想做的是在我的kivy应用程序,当我按下一个按钮时,我想开始使用这个coed来生成一个训练数据库,所以当我调用识别器时,它将使用这个训练数据库来获取人脸特征并识别人脸。我想把这个代码放在一个长方形中,这样当它拍摄完图片后,我就可以按下右下角的一个按钮进入下一页。所以我应该定义一个函数,然后在特定屏幕上的kv代码中,我必须放置BoxLayout,在这个BoxLayout中,我应该调用该函数,我在写吗?您可以独立于屏幕定义函数,只需在屏幕上调用或引用它。在我提供的示例中,FaceGenerator函数与kv或类无关。我只是在类中调用它,调用函数并使用结果更改小部件的属性。你的屏幕也可以这样做。调用函数并使用结果更新屏幕上的内容,例如屏幕标签的文本属性。好的,我会试试这个,让你知道我发生了什么。ThanksBoxLayout:orientation:“horizontal”FaceGenerator()不起作用,因为您需要一些按钮来调用函数。此外,kivy在另一个函数运行时停止更新,因此您可能需要研究线程问题,如果我在BoxLayout中添加了一个按钮,并且当我按下该按钮时,它打开该功能并开始拍摄一些照片。我可以在发布时执行吗:app.root.current=“FaceGenerator”这仍然不是对函数的调用。在发布时执行类似操作:app.root.fg()并将方法def fg(self):FaceGenerator()添加到你的应用类中你的意思是这样做,类FaceRecognitionApp(app):def fg(self):FaceGenerator()def build(self):return sm
      ScreenManagement:
          id: screen_management
          ScreenOne:
          ScreenTwo:
          ScreenThree:
      
      
      
      <ScreenOne>:
          name: "screen1"
          id: screen_one
      
          FloatLayout:
              canvas.before:
                  Rectangle:
                      source: "image1.jpg"
                      pos:self.pos
                      size:self.size
      
              Label:
                  text:"Hello\n Welcome to my App\n"
                  font_size:40 
                  color: 0,0,0,1
              Button:
                  text: 'Next'
                  font_size: 32 # font size
                  size_hint: .2,.1
                  pos_hint:{'right':1, 'y':0}
                  on_release: app.root.current="screen2"
      
      <ScreenTwo>:
          name: "screen2"
          id: screen_two
      
          FloatLayout:
              canvas:
      
                  Rectangle:
                      source: "image1.jpg"
                      pos:self.pos
                      size:self.size
      
              Label:
                  text:"Please insert your Name\n Please insert your Password\n"
                  font_size:40 
                  color: 0,0,0,1
              Button:
                  text: 'Next'
                  font_size: 32 # font size
                  size_hint: .2,.1
                  pos_hint:{'right':1, 'y':0}
                  on_release: app.root.current="screen3"
      
      <ScreenThree>:
          name: "screen3"
          id: screen_three
      
      
          FloatLayout:
              canvas:
      
                  Rectangle:
                      source: "image1.jpg"
                      pos:self.pos
                      size:self.size
      
      
              Button:
                  text: 'Next'
                  font_size: 32 # font size
                  size_hint: .2,.1
                  pos_hint:{'right':1, 'y':0}
                  on_release: app.root.current="screen1"
      
              BoxLayout:
                  orientation: 'horizontal'
                  FaceGenerator()