0

I wrote a simple app that I want to print logging message every seconds. I open a terminal and run it, it works well. But when I use mouse to click the terminal, it will not print logging message unless I print Enter in the terminal.

The Code

import logging
from PyQt5.QtCore import *

logging.basicConfig(level=logging.DEBUG)


app = QCoreApplication([])

timer = QTimer()
timer.setInterval(1000)
timer.timeout.connect(lambda: logging.info("abc"))
timer.start()

app.exec()
Mustafa Kemal
  • 762
  • 1
  • 5
  • 11
jett chen
  • 1,067
  • 16
  • 33

1 Answers1

1

Seems to be environment issue. Below example demonstrates that Qt logging works fine even if mouse button is pressed.

cat pyqt_logging_ex.py
#!/usr/bin/python3.9
import logging,os
from PyQt5.QtCore import *
from pynput import mouse,keyboard

def on_click(x, y, button, pressed):
    print("Mouse click")

def on_press(key):
    print("Key pressed, exiting")
    os._exit(0)

listener = keyboard.Listener(on_press=on_press)
listener.start()
listener = mouse.Listener(on_click=on_click)
listener.start()
print("Started listeners")

logging.basicConfig(level=logging.DEBUG)
app = QCoreApplication([])
timer = QTimer()
timer.setInterval(1000)
timer.timeout.connect(lambda: logging.info("abc"))
timer.start()
app.exec()

Run example:

pyqt_logging_ex.py
Started listeners
INFO:root:abc
INFO:root:abc
INFO:root:abc
Mouse click
Mouse click
INFO:root:abc
INFO:root:abc
Mouse click
Mouse click
INFO:root:abc
INFO:root:abc
qKey pressed, exiting

My environment:

uname -or ; lsb_release -d ; python3.9 --version
4.4.0-19041-Microsoft GNU/Linux
Description:    Ubuntu 20.04.3 LTS
Python 3.9.5
  • Your solution filter windows mouse and key event, Thanks for your help. – jett chen Sep 27 '21 at 11:54
  • If the OP is using the Windows' command prompt, then it's due a "feature": see [Why is my command prompt freezing on Windows 10?](https://stackoverflow.com/q/33883530) – musicamante Sep 27 '21 at 15:04
  • I'm familiar with this "feature". It is not relevant for my answer, as I'm using X terminal. It is a good question, as operating system type has not been mentioned in the issue description. – Dmitry Messerman Sep 27 '21 at 15:38
  • @DmitryMesserman since it's unlikely that this behavior would happen on Linux, I supposed the problem was related to that, and the OP just confirmed it in the comment to the question. – musicamante Sep 27 '21 at 15:47