Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果我没有';你不能动我的鼠标吗?_Python_Multithreading_Tkinter - Fatal编程技术网

Python 如果我没有';你不能动我的鼠标吗?

Python 如果我没有';你不能动我的鼠标吗?,python,multithreading,tkinter,Python,Multithreading,Tkinter,我已经设法通过线程执行了这个函数,但是过了一会儿它就停止工作了,直到我再次移动鼠标,如果我最小化窗口,线程就会停止,直到我重新打开窗口 import tkinter as tk,tkinter.ttk as ttk, pyautogui, numpy as np, easygui, cv2, os, sys, re, math, subprocess, threading, requests, PIL.Image, random, time 函数我正在尝试运行线程 def runScript

我已经设法通过线程执行了这个函数,但是过了一会儿它就停止工作了,直到我再次移动鼠标,如果我最小化窗口,线程就会停止,直到我重新打开窗口

import tkinter as tk,tkinter.ttk as ttk, pyautogui, numpy as np, easygui, cv2, os, sys, re, math, subprocess, threading, requests, PIL.Image, random, time
函数我正在尝试运行线程

 def runScript(self):
        def on_release(key):
            if key == keyboard.Key.esc:
                print('test')
                return False
        listener = keyboard.Listener(
            on_release=on_release)
        listener.start()
        
##        self.toplevel1.iconify()
        e1 = lb_0.get()
        if loopVar.get() == True:  
                if moveVar.get() == True and randVar.get() == True:
                        while loopVar.get() == True:
                            for i in range(len(e1)):
                                e1, e2, e3, e4 = lb_0.get(), lb_1.get(), lb_2.get(), lb_3.get()
                                xyCoor1 = [int(s) for s in re.findall(r'\b\d+\b', str(e1[i]))]
                                xyCoor2 = [int(s) for s in re.findall(r'\b\d+\b', str(e2[i]))]
                                cCoor1 = [int(s) for s in re.findall(r'\b\d+\b', str(e3[i]))]
                                iCoor1 = [int(s) for s in re.findall(r'\b\d+\b', str(e4[i]))]
                                randLoc = int(randText.get().strip())
                                randX1 = int(xyCoor1[0] - int(randLoc) / 2)
                                print('w')
                                randX2 = int(xyCoor1[0] + int(randLoc) / 2)
                                randY1 = int(xyCoor2[0] - int(randLoc) / 2)
                                randY2 = int(xyCoor2[0] + int(randLoc) / 2)
                                trueX = int(random.randint(randX1,randX2))
                                trueY = int(random.randint(randY1,randY2))
                                print(trueX)
                                print(trueY)
                                pyautogui.PAUSE = iCoor1[0]
                                pyautogui.moveTo(x=trueX, y=trueY, duration=0.05, _pause=False)
                                pyautogui.click(x=trueX, y=trueY, clicks=cCoor1[0], button='left')
    def threadRunScript(self, event):
        t = threading.Thread(target=self.runScript)
        t.daemon = True
        t.start()
    pass
创建线程的函数

 def runScript(self):
        def on_release(key):
            if key == keyboard.Key.esc:
                print('test')
                return False
        listener = keyboard.Listener(
            on_release=on_release)
        listener.start()
        
##        self.toplevel1.iconify()
        e1 = lb_0.get()
        if loopVar.get() == True:  
                if moveVar.get() == True and randVar.get() == True:
                        while loopVar.get() == True:
                            for i in range(len(e1)):
                                e1, e2, e3, e4 = lb_0.get(), lb_1.get(), lb_2.get(), lb_3.get()
                                xyCoor1 = [int(s) for s in re.findall(r'\b\d+\b', str(e1[i]))]
                                xyCoor2 = [int(s) for s in re.findall(r'\b\d+\b', str(e2[i]))]
                                cCoor1 = [int(s) for s in re.findall(r'\b\d+\b', str(e3[i]))]
                                iCoor1 = [int(s) for s in re.findall(r'\b\d+\b', str(e4[i]))]
                                randLoc = int(randText.get().strip())
                                randX1 = int(xyCoor1[0] - int(randLoc) / 2)
                                print('w')
                                randX2 = int(xyCoor1[0] + int(randLoc) / 2)
                                randY1 = int(xyCoor2[0] - int(randLoc) / 2)
                                randY2 = int(xyCoor2[0] + int(randLoc) / 2)
                                trueX = int(random.randint(randX1,randX2))
                                trueY = int(random.randint(randY1,randY2))
                                print(trueX)
                                print(trueY)
                                pyautogui.PAUSE = iCoor1[0]
                                pyautogui.moveTo(x=trueX, y=trueY, duration=0.05, _pause=False)
                                pyautogui.click(x=trueX, y=trueY, clicks=cCoor1[0], button='left')
    def threadRunScript(self, event):
        t = threading.Thread(target=self.runScript)
        t.daemon = True
        t.start()
    pass
指向功能的按钮

        #Run script button
        self.button3 = tk.Button(self.labelframe1)
        self.button3.configure(activeforeground='#000000', font='{Century Gothic} 13 {}', foreground='#fefffe', highlightbackground='#393a4c')
        self.button3.configure(highlightcolor='#2e2a2e', highlightthickness='100', justify='center', relief='raised', command=self.threadRunScript)
        self.button3.configure(image=imgToButton4("/Users/giuseppemarziano/Desktop/Scripts/Personal Projects/Auto Click GM/ACGM 0.04/ACGM Images/start.svg"))
        self.button3.place(anchor='nw', height='25', relx='0.0', rely='0.0', width='25', x='320', y='4')
        self.button3.bind('<1>', self.threadRunScript, add='+')
主回路

if __name__ == '__main__':
    app = Acgm003App()
    app.run()
我曾尝试使用
queue.queue
,但它不允许我在不传递参数的情况下使用,我也尝试过类似于
self.toplevel1.after(1000,self.update())
,但它无法正常工作


有没有一种简单的方法可以让函数继续运行,或者以给定的时间间隔刷新gui以允许线程不被中断?

因此我通过在主类中添加以下内容来修复它:

self.mainwindow.after(10, self.updateMy)

    def updateMy(self):
        self.mainwindow.after(10, self.updateMy)
    pass
上面的代码将每隔10毫秒创建一个循环,以便
根目录更新自身


我不确定这是否会导致资源方面的复杂性。

没有完全阅读,但这是正确的方法(在大多数情况下):
self.toplevel1.after(1000,self.update)
@Matiss,我应该把它放在哪里?在我构建对象的主函数中,或者在我想要线程的函数中?
tkinter
是单线程的,不建议在其上使用线程。@CoolCloud,阻止窗口冻结的最佳方法是什么?使用另一个线程:pGlad您找到了解决方案:D