python 3中的类未按预期工作

python 3中的类未按预期工作,python,class,python-3.x,Python,Class,Python 3.x,我已经开始开发一个程序,允许用户使用raspberry pi相机轻松拍照。我遇到的问题是,我创建的类没有达到我预期的效果。当代码运行时没有输出,应该运行Picture\u Name\u Settings函数。我还是个新手,所以我可能错过了一些简单的东西,而我在网上读到的教程并不能解决我的问题。下面是我的代码: import picamera, time class CameraController: def _init_(self): pass def Pic

我已经开始开发一个程序,允许用户使用raspberry pi相机轻松拍照。我遇到的问题是,我创建的类没有达到我预期的效果。当代码运行时没有输出,应该运行Picture\u Name\u Settings函数。我还是个新手,所以我可能错过了一些简单的东西,而我在网上读到的教程并不能解决我的问题。下面是我的代码:

import picamera, time

class CameraController:
    def _init_(self):
        pass

    def Picture_Name_Settings(self, user_name, automatic_name, name_setting):
        pic_name = input("To name the image input N\nTo use automatic naming input A ")
        pic_name = pic_name.upper()
        if pic_name == "N":
            user_name = input("Please enter the name of what you want your image to be called ")
            name_setting = 1

        if pic_name == "A":
            current_time = (time.strftime("%H:%M:%S"))
            current_date = (time.strftime("%d:%m:%Y"))
            automatic_name = "".join(("Image taken at ", current_time, " on the ", current_date, ".jpg"))
            name_setting = 2

    def Camera_Capture(self):
        self.Picture_Name_Settings(user_name, automatic_name, name_settings)
        if name_setting == 1:
            picture_name = user_name
        if name_setting == 2:
            picture_name = automatic_name

        camera = picamera.PiCamera()
        camera.capture(picture_name)

将user_name、automatic_name和name_设置变量声明为实例变量,这样就可以了

class CameraController:
def _init_(self):
    pass

def Picture_Name_Settings(self):
    pic_name = input("To name the image input N\nTo use automatic naming input A ")
    pic_name = pic_name.upper()
    if pic_name == "N":
        self.user_name = input("Please enter the name of what you want your image to be called ")
        self.name_setting = 1

    if pic_name == "A":
        current_time = (time.strftime("%H:%M:%S"))
        current_date = (time.strftime("%d:%m:%Y"))
        self.automatic_name = "".join(("Image taken at ", current_time, " on the ", current_date, ".jpg"))
        self.name_setting = 2

def Camera_Capture(self):
    self.Picture_Name_Settings()
    if self.name_setting == 1:
        picture_name = self.user_name
    if self.name_setting == 2:
        picture_name = self.automatic_name

    camera = picamera.PiCamera()
    camera.capture(picture_name)

您只定义了一个类。定义类不会运行任何代码。这就像定义一个函数如何运行这个类?您好-除非您确定您是指init,否则很可能您希望第4行有一个双下划线-因此def _init_self:变成def _init_self。_uuuinit_uuu方法的基本运行说明。我理解您所做的,但它并不能解决函数not runningcontroller=CameraController controller.Camera_Capture的问题。这应该调用该方法,所以我将它们键入空闲窗口中?不,您将其添加到python文件的底部。没有压痕。分两行:controller=CameraController和controller.Camera\u Capture您应该了解类是如何工作的,类定义是什么,以及如何在类中调用方法。