函数仅在关闭tkinter窗口后运行

2024-09-30 08:37:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习如何用Python编程和使用PIL(枕头)来创建能够识别屏幕变化的小机器人,以便执行重复命令(用pynput模块按键盘)。你知道吗

我用Tkinter做了一个小GUI,以便获得用户在使用bot时要使用的参数(Entry widget)的相关信息,我还需要一些按钮来启用/禁用代码的某些部分(我使用check按钮和布尔逻辑来启用/禁用)以及启用机器人的主要功能(当它识别到屏幕上的某个变化时按下按钮)。你知道吗

我现在经历的是,一旦我运行我的程序,它就会打开我的GUI和所有用户会看到的东西,但它不会执行我的主功能,它只会在我关闭GUI后执行主功能。 我很想公布我的代码,但它超过220行,它可能会变得更加混乱。你知道吗

所以,长话短说,这是一种错误,当没有经验的程序员试图用tkinter创建软件时经常发生的错误吗?如果是,是什么原因造成的,如何修复?你知道吗

我很感激你们能帮我什么忙。:]

    ...creates frames, widgets, entries...

    mainHealerVar = IntVar() 
    main_healer_check = Checkbutton(root, text="Enable the bot", variable=mainHealerVar) #this should mean that when i press this checkbutton it'll enable the bot and perform the repetitive commands for as long as it stays checked

    ... some more buttons/code...


    root.mainloop() #right after I stop setting up the GUI, I place this function

    ... make logic in order to know which part of the repetitive functions to execute...

    if mainHealerVar.get() == 1:
        mainHealFunction()    #function to check if the checkbutton is still pressed (if it is, it should execute mainHealFunction()

它应该在我单击启用检查按钮时立即由pynput模块执行重复命令,但是它不会执行任何操作,直到我关闭GUI,然后它开始执行重复命令。我也尝试过在结尾使用while循环,而不是“if”,但最终得到了相同的结果。你知道吗


Tags: theto命令功能if屏幕checkbot

热门问题