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()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…