Python - 如果在 if 内

Python - if inside of an if

我的脚本通过第一个循环查找某些蓝牙状态标准(如果它已打开并且没有连接设备)然后休眠 10 秒以允许用户更改蓝牙状态然后再次检查相同的标准在执行提示之前。

我不明白的是,当我 运行 代码并在等待时间(10 秒)结束之前更改状态时,它仍然 运行 是第二个 if 中的代码声明。

def bluetoothLoop():
    while True:        
        def bluetooth_msg():
            BT_state = subprocess.run(['''system_profiler SPBluetoothDataType'''], shell=True, capture_output=True, encoding="utf", errors="ignore")
            BT_state = BT_state.stdout
            sound = "Blow"
            title = "TURN OFF BLUETOOTH"
            message = "Wasting energy"
            if "State: On" in BT_state and not "  Connected:" in BT_state:
                time.sleep(10)
                if "State: On" in BT_state and not "  Connected:" in BT_state:
                    command = f'''
                    osascript -e 'display notification "{message}" with title "{title}" sound name "{sound}"'
                    '''
                    os.system(command)
        bluetooth_msg()

你休眠 10 秒后没有刷新状态。尝试

if "State: On" in BT_state and not "  Connected:" in BT_state:
                time.sleep(10)
                BT_state = subprocess.run(['''system_profiler SPBluetoothDataType'''], shell=True, capture_output=True, encoding="utf", errors="ignore")
                BT_state = BT_state.stdout
                if "State: On" in BT_state and not "  Connected:" in BT_state:
                    command = f'''
                    osascript -e 'display notification "{message}" with title "{title}" sound name "{sound}"'
                    '''
                    os.system(command)

您甚至可以通过

清理您的代码
def bluetoothLoop():
    BT_state = subprocess.run(['''system_profiler SPBluetoothDataType'''], shell=True, capture_output=True, encoding="utf", errors="ignore")
    BT_state = BT_state.stdout
    def bluetooth_msg():
            sound = "Blow"
            title = "TURN OFF BLUETOOTH"
            message = "Wasting energy"
            command = f'''
                    osascript -e 'display notification "{message}" with title "{title}" sound name "{sound}"'
                    '''
            os.system(command)
    while "State: On" in BT_state and not "  Connected:" in BT_state:
        bluetooth_msg()
        time.sleep(10)
        BT_state = subprocess.run(['''system_profiler SPBluetoothDataType'''], shell=True, capture_output=True, encoding="utf", errors="ignore")
        BT_state = BT_state.stdout

请注意我没有 运行 上面的代码,因此您可能需要稍微调整一下。

你 运行 subprocess 在睡眠前获得 BT_state 但你不会 运行 在睡眠后再次获得它所以变量 BT_state 不更新。实际上,您正在检查同一件事两次。

我在下面重构了您的代码以分离出不同的部分,希望能更容易地看到发生了什么。

import subprocess
import time


def check_bt_status():
    output = subprocess.check_output(["system_profiler",
                                      "SPBluetoothDataType"])
    return all([b"Bluetooth Power: On" in output,
                b"Connected: Yes" not in output])
    

def show_msg(title, message, sound):
    apple_script = (f'display notification "{message}"'
                    f'with title "{title}" '
                    f'sound name "{sound}"')
    subprocess.check_output(["osascript", "-e", apple_script])


def bluetooth_loop():
    while True:
        if check_bt_status():
            show_msg("TURN OFF BLUETOOTH", "Wasting energy", "Blow")
        time.sleep(10)

if __name__ == "__main__":
    bluetooth_loop()

将BT状态逻辑放在一个单独的方法中意味着如果你需要改变那个逻辑,那么你只需要在一个地方改变它。