python 3中的类没有按预期工作

时间:2015-04-09 11:17:02

标签: python class python-3.x

我已经开始研究一个允许用户使用树莓派照相机轻松拍照的程序。我遇到的问题是我创建的课程没有达到我的预期。当代码运行时没有输出,应该运行Picture_Name_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)

1 个答案:

答案 0 :(得分:0)

将user_name,automatic_name和name_setting变量声明为实例变量,你应该没问题

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)