Python 布尔值切换

发布于 2025-01-10 09:51:49 字数 431 浏览 0 评论 0原文

我想制作一个自动点击器,但我的切换脚本不起作用。如果我按一次该键,它就会启动。当我再次按下它时,什么也没有发生。我并不是一个真正优秀的Python程序员,我也不知道哪里出了问题。

这是我的脚本:

import keyboard
import pyautogui

Running = False
while True:
    if keyboard.is_pressed("F2"):
        if Running:
            Running = False
        elif Running == False:
            Running = True
    
    if Running:
        pyautogui.click()
    if keyboard.is_pressed("t"):
        exit

I want to make an auto clicker, but I have the problem that my toggle script doesn't work. if I press the key once, it starts. when I press it again nothing happens. I'm not really a good python programmer and I am clueless about what is wrong.

Here is my Script:

import keyboard
import pyautogui

Running = False
while True:
    if keyboard.is_pressed("F2"):
        if Running:
            Running = False
        elif Running == False:
            Running = True
    
    if Running:
        pyautogui.click()
    if keyboard.is_pressed("t"):
        exit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

本宫微胖 2025-01-17 09:51:49

你可以这样做:

import keyboard
import pyautogui

Running = 0
while True:
    if keyboard.is_pressed("F2"):
        Running ^= 1 # if Running is 0 it will become 1. If it's 1 it will become 0
    
    if Running:
        pyautogui.click()
    if keyboard.is_pressed("t"):
        exit()

You could do it like this:

import keyboard
import pyautogui

Running = 0
while True:
    if keyboard.is_pressed("F2"):
        Running ^= 1 # if Running is 0 it will become 1. If it's 1 it will become 0
    
    if Running:
        pyautogui.click()
    if keyboard.is_pressed("t"):
        exit()
动次打次papapa 2025-01-17 09:51:49

只需在 Python 中使用 not 运算符

while True:
    if keyboard.is_pressed("F2"):
            Running = not Running

Just use the not operator in Python

while True:
    if keyboard.is_pressed("F2"):
            Running = not Running
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文