Python - 程序无法识别变量中的值
Python - Program wont recognize values in variable
我看不出有什么问题,但是它不会执行该程序的最后一部分。我可以张贴它需要识别的图片,但它什么也做不了。一旦某个图片弹出,它应该保持 f 一段时间,但程序需要 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()
我认为您可能希望将 stat
变量检查放在主循环中。从我提供的代码中可以看出,在启动主 window 循环之前,您只检查 if 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, 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()
我看不出有什么问题,但是它不会执行该程序的最后一部分。我可以张贴它需要识别的图片,但它什么也做不了。一旦某个图片弹出,它应该保持 f 一段时间,但程序需要 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()
我认为您可能希望将 stat
变量检查放在主循环中。从我提供的代码中可以看出,在启动主 window 循环之前,您只检查 if 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, 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()