簡體   English   中英

python 3中的類無法按預期工作

[英]class in python 3 not working as expected

我已經開始研究一個程序,該程序將使用戶可以使用樹莓派pi相機輕松拍照。 我遇到的問題是我創建的類沒有達到我的預期。 當代碼運行時,沒有輸出,應該發生的是應該運行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)

將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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM