python - 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()
解决方案
我认为您可能希望将stat
变量检查放在主循环中。从我从提供的代码中可以看出,您只stat == 1
在开始主窗口循环之前检查一次是否,这意味着它永远不会在您单击按钮时进行检查。更改为的函数stat
似乎1
也被覆盖了。statbutton = Button(window, text="Enable", command=ChangeStatus1)
已创建,但后来设置为statbutton = Button(window, text="Disable", command=ChangeStatus2)
,将 stat 保留为 0。也许尝试将这一秒重命名statbutton
为其他内容。虽然我不熟悉您使用的库,但这些可能不是问题。
推荐阅读
- c# - C# windows 应用程序错误“输入字符串的格式不正确”
- xamarin - 在 Xamarin.Forms 中本地化应用程序并在运行时更改 FlowDirection?
- php - 如何通过 PHP 执行命令?
- javascript - cache.match() 返回响应但没有标头
- php - mysqli_query(): MySQL 服务器已经消失
- regex - 正则表达式如何匹配另一种模式?
- azure - 使用标头变量进行用户身份验证
- python - 遍历 XML 中的特定子节点并使用 Python 保存到 CSV
- batch-file - 在比较两个文件的差异时,是否可以在比较 result.txt 中添加换行符
- html - 如何在下拉菜单上保留 Django 模板中的数据?