Search code examples
pythonpyautogui

Python - Program wont recognize values in variable


I see nothing wrong with this, however it wont execute the last part of this program. I can put up the picture that it needs to recognise but it won't do anything. It's supposed to hold f for a certain amount of time once a certain picture pops up but the program needs to be enabled/disabled.

#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()

Solution

  • It seems like you need quite a complex state machine to cover all the possibilities. I have added def states(): to handle them:

    #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, 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():
            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, 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
        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()