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,text="txt")
    li.grid(row=4,text="txt")
    li.grid(row=5,text="Status: ")
    li.grid(row=6,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()

解决方法

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

,

似乎您需要一个相当复杂的状态机来涵盖所有可能性。我添加了 def states(): 来处理它们:

#Import all python librabries

stat = 0

# global req   # 0=>nop 1=>active 2=>passive
req = 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,text="txt")
    li.grid(row=4,text="txt")
    li.grid(row=5,text="Status: ")
    li.grid(row=6,text="Alive")
    li.grid(row=6,column=1)
    #Button to activat
    def ChangeStatus1():
        global req
        li=Label(window,text="Enabled")
        li.grid(row=1,column=1)
        req = 1
        states()

    def ChangeStatus2():
        global req
        li=Label(window,text="Disabled")
        li.grid(row=1,column=1)
        req = 2
        states()

    statbutton = Button(window,command=ChangeStatus1)
    statbutton.grid(row=2,column=0)
    statbutton = Button(window,command=ChangeStatus2)
    statbutton.grid(row=2,column=2)

    #entry's
    def states():
        global stat
        global req
        local_req = req
        req = 0
        if stat == 0:
            if local_req != 1:
               return
            stat = 1
        if stat == 1:
            if local_req == 2:
                stat = 0
                return
            if pyautogui.locateOnScreen('img.png'):
                stat = 2
                li=Label(window,text="txt")
                li.grid(row=6,column=1)
                keyboard = Controller()
    
                keyboard.press('f')
                window.after(8000,states)
            else:
                window.after(100,states)
        elif stat == 2:
            keyboard = Controller()
            keyboard.release('f')
            if local_req == 2:
               stat = 0
               return
            stat = 3
            window.after(100,states)
        elif stat == 3:
            if local_req == 2:
               stat = 0
               return
            if not pyautogui.locateOnScreen('img.png'):
                stat = 1
                li=Label(window,text="Alive")
                li.grid(row=6,column=1)
    
            window.after(100,states)

            
    window.mainloop()
Main_window()