Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9143757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:06:13+00:00 2026-06-17T10:06:13+00:00

I have written a short program here designed to record and play back my

  • 0

I have written a short program here designed to record and play back my mouse motions. I’ve implemented a simple GUI in PyQt and had it working in the past. Recently I decided to update the code to make it less ‘hacky’ by implementing QThread instead of using processEvents() in a main loop.

The code below executes, but behaves strangely. When running the code, here is what happens:

Input 1: I press Record

Console Output: isRecording = True

Input 2: I press Stop

Console Output: Stopped!

Input 3: I press Play

Console Output: isRecording = True

Input 4: I press Stop

Console Output:
Stopped!
Play!
Playback complete!

I am unclear why after pressing play the program attempts to record again, and only after pressing stop does it begin playback. Furthermore, after this stretch of weirdness, it continues to exhibit this similar type of output with minor deviations.

My guess is it has something to do with reassigning the thread signal started depending on whether or not I am calling the play or record member function of the CursorCapture class.

Any insight would be greatly appreciated.

Code

import win32api
import sys
import time

from PyQt4 import QtGui
from PyQt4.QtCore import Qt, QPoint
from PyQt4 import QtCore

class MouseRecord(QtCore.QObject):
    
    finished = QtCore.pyqtSignal()    
    
    def __init__(self):
        
        super(MouseRecord, self).__init__()        
        
        self.isRecording = False
        self.cursorPath = []
    
    @QtCore.pyqtSlot()  
    def record(self):
    
        self.isRecording = True
        self.cursorPath = []
        print "isRecording = " + str(self.isRecording)

        while(self.isRecording):
        
            #print "Recording"            

            self.cursorPath.append(win32api.GetCursorPos())
            time.sleep(.02)            
            
        self.finished.emit()
        print "Stopped!"
    
    def stop(self):
    
        self.isRecording = False
    
    @QtCore.pyqtSlot()    
    def play(self):
        
        print "Play!"

        for pos in self.cursorPath:
            #print "Playing"            
            win32api.SetCursorPos(pos)
            time.sleep(.02)        
            
        print "Playback complete!"
        self.finished.emit()            
        

class CursorCapture(QtGui.QWidget):
    
    def __init__(self):
        
        super(CursorCapture, self).__init__()
        
        self.isRecording = False
        self.mouseRecorder = MouseRecord()
        
        self.myThread = QtCore.QThread()
                
        self.mouseRecorder.moveToThread(self.myThread)
        self.mouseRecorder.finished.connect(self.myThread.quit) 
                        
        self.initUI()
        
    def initUI(self):
        
        self.recordBtn = QtGui.QPushButton("&Record")
        self.stopBtn   = QtGui.QPushButton("&Stop")
        self.playBtn   = QtGui.QPushButton("&Play")        
        
        self.recordBtn.clicked.connect(self.record)
        self.stopBtn.clicked.connect(self.stop)
        self.playBtn.clicked.connect(self.play)
        
        self.hBox = QtGui.QHBoxLayout()
        
        self.hBox.addWidget(self.recordBtn)
        self.hBox.addWidget(self.stopBtn)
        self.hBox.addWidget(self.playBtn)
        
        self.setLayout(self.hBox)
        
        self.setWindowTitle("Cursor Capture")
        self.show()
        
    def record(self):
        
        self.myThread.started.connect(self.mouseRecorder.record)
        self.myThread.start()        
        
    def stop(self):
        
        self.mouseRecorder.stop()
        
    def play(self):
        
        self.myThread.started.connect(self.mouseRecorder.play)
        self.myThread.start()

def main():

    qApp = QtGui.QApplication(sys.argv)
    
    cursorCapture = CursorCapture()
    
    sys.exit(qApp.exec_())    
            
if __name__ == "__main__":
    main()    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:06:14+00:00Added an answer on June 17, 2026 at 10:06 am

    After more investigating and playing around I discovered the problem. When reconnecting signals to slots, you have to make sure to disconnect() the previous signals. What was happening was:

    Press record. Record signal is connected.
    Press play. Since record signal wasn’t disconnected, it fires first. THEN, the play signal gets connected and executed. This type of behavior stacks, and this is why things were acting so strangely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a really short program written in boost::xpressive #include <iostream> #include <boost/xpressive/xpressive.hpp> using
I have just written this short C++ program to approximate the actual number of
I have written a short Scala program to read a large file, process it
here i have written a program on (3n+1) problem.it's also a problem from UVa.
I have written two short tests and compiled both with g++ -S (gcc version
I have written a java program generates lots of files (say txt files) in
So I have written a heap-sort program in C++ which takes in an array
I have written a mail-processing program, which basically slaps a template on incoming mail
I have written a BubbleSort program and it works great, gives me a good
I have written a small example C++ program, using boost::thread. Since it's 215 lines,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.