首页 > 解决方案 > Python - 程序不会识别变量中的值

问题描述

我认为这没有任何问题,但是它不会执行该程序的最后一部分。我可以提出它需要识别的图片,但它不会做任何事情。一旦弹出某个图片,它应该保持 f 一段时间,但需要启用/禁用程序。

#Import all python librabries

global stat
stat = 0

def Main_window():
    #Create window object
    window=Tk()

    #program status

    li=Label(window, text="SRA Version 1")
    li.grid(row=0, column=0)

    li=Label(window, text="text")
    li.grid(row=0, column=2)

    #status
    li=Label(window, text="Disabled")
    li.grid(row=1, column=1)

    li=Label(window, text="text")
    li.grid(row=3, column=0)

    li=Label(window, text="text")
    li.grid(row=3, column=1)

    li=Label(window, text="txt")
    li.grid(row=4, column=1)

    li=Label(window, text="txt")
    li.grid(row=5, column=1)

    li=Label(window, text="Status: ")
    li.grid(row=6, column=0)

    li=Label(window, text="Alive")
    li.grid(row=6, column=1)
    #Button to activat
    def ChangeStatus1():
        li=Label(window, text="Enabled")
        li.grid(row=1, column=1)
        stat = 1

    def ChangeStatus2():
        li=Label(window, text="Disabled")
        li.grid(row=1, column=1)
        stat = 0

    statbutton = Button(window, text="Enable", command=ChangeStatus1)
    statbutton.grid(row=2, column=0)
    statbutton = Button(window, text="Disable", command=ChangeStatus2)
    statbutton.grid(row=2, column=2)
    #entry's
    if stat == 1:
        if pyautogui.locateOnScreen('img.png'):
            li=Label(window, text="txt")
            li.grid(row=6, column=1)
            keyboard = Controller()
            key = "f"

            keyboard.press(key)
            time.sleep(8)
            keyboard.release(key)
        else:
            li=Label(window, text="Alive")
            li.grid(row=6, column=1)
            
    window.mainloop()
Main_window()

标签: pythonpyautogui

解决方案


我认为您可能希望将stat变量检查放在主循环中。从我从提供的代码中可以看出,您只stat == 1在开始主窗口循环之前检查一次是否,这意味着它永远不会在您单击按钮时进行检查。更改为的函数stat似乎1也被覆盖了。statbutton = Button(window, text="Enable", command=ChangeStatus1)已创建,但后来设置为statbutton = Button(window, text="Disable", command=ChangeStatus2),将 stat 保留为 0。也许尝试将这一秒重命名statbutton为其他内容。虽然我不熟悉您使用的库,但这些可能不是问题。


推荐阅读