Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

python - PySide2 app crashes if import pynput in source code

The first important file in my app is listener.py:

from PySide2.QtCore import QRunnable, QObject, Signal, Slot
from pynput import mouse
from pynput.keyboard import Key, Controller

controller = Controller()

class ListenerSignal(QObject):
    finish = Signal()

class Listener(QRunnable):
    def __init__(self):
        super().__init__()

        self.signal = ListenerSignal()

    def on_click(self, x, y, button, pressed):
        if button == mouse.Button.middle and pressed:
            controller.press(Key.ctrl)
            controller.press('c')
            controller.release('c')
            controller.release(Key.ctrl)

    @Slot()
    def run(self):
        try:
            self.listener_mouse = mouse.Listener(on_click=self.on_click)
            self.listener_mouse.start()
        except:
            pass
        else:
            self.signal.finish.emit()

The second important file is main.py:

from fbs_runtime.application_context.PySide2 import ApplicationContext, cached_property

from PySide2.QtCore import Qt, QSize, QThreadPool
from PySide2.QtWidgets import QAction, QApplication, QCheckBox, QHBoxLayout, QMainWindow, QMenu, QPlainTextEdit, QPushButton, QShortcut, QSizePolicy, QSpacerItem, QStatusBar, QSystemTrayIcon, QToolBar, QVBoxLayout, QWidget
from PySide2.QtGui import QIcon, QKeySequence

from listener import Listener

class MainWindow(QMainWindow):
    """
      some code here
    """

class AppContext(ApplicationContext):
    @cached_property
    def main_window(self):
        return MainWindow(self)

    def run(self):
        self.main_window.show()

        """ PAY ATTENTION HERE """
        self.threadpool = QThreadPool()
        listener = Listener()
        listener.signal.finish.connect(self.main_window.do_something)
        self.threadpool.start(listener)
        """ ------------------ """

        return self.app.exec_()


if __name__ == '__main__': 
    appctxt = AppContext()
    exit_code = appctxt.run()
    sys.exit(exit_code)

One thing is confusing if I ran the command fbs run in terminal. My app worked very well, no bugs, no errors reported on terminal. But when I ran command fbs freeze and then fbs installer to export to file app.deb. I have installed it on my laptop and then open it in apps menu. There aren't any windows, errors or messages appears. I checked my source code and I realized if I don't import file listener.py into file main.py and comment the code segment PAY ATTENTION HERE (I marked it in main.py ) and then run commands fbs freeze, fbs installer and install it again, my app works well.

Note:

  • Operating system: Ubuntu 18.04
  • Python's version: 3.6.9

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...