How can I make my PyQt5 app oney one instance?

梦想的初衷 提交于 2021-01-29 15:42:56

问题


What I want to achieve:

  • when I run application from start menu, app starts (if app not running).
  • If an app is already running, then don't create another instance, just show the previous running app window.

What I've tried:

  • created a .txt file in a directory, write 'running' & 'not running' into the file while opening & exiting the window. And checking the file contents at very start. For ex: When starting app: 1.) Check the file content, 2.) If file content is 'running', show warning & exit the app 3.) If file content is 'not running', write 'running' into the file & start app 4.) Write 'not running' into the file on exit.

Issues I'm facing:

  • This method doesn't feel like the right way to achieve this.

  • If file item says 'not running', app shows warning and exits. While I want it to show already running instance.

  • File items not updated to 'not running' when app exits because of some error in code (because that part is never reached due to error)

Can anybody please help me out regarding this?


回答1:


You can achieve something similar with the win32gui module. To install it type into CMD pip install pywin32. Now this is the code:

from win32 import win32gui
import sys

def windowEnumerationHandler(hwnd, top_windows):
    top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))

top_windows = []
win32gui.EnumWindows(windowEnumerationHandler, top_windows)
for i in top_windows:
    if "Program" in i[1]: #CHANGE PROGRAM TO THE NAME OF YOUR WINDOW
        win32gui.ShowWindow(i[0],5)
        win32gui.SetForegroundWindow(i[0])
        sys.exit()


from PyQt5.QtWidgets import QApplication, QWidget

def main():

    #YOUR PROGRAM GOES HERE

    app = QApplication(sys.argv)

    w = QWidget()
    w.setGeometry(500, 500, 500, 500)
    w.setWindowTitle('Simple')
    w.show()

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Basically, at the beginning, the program gets the name of every open window. If the window name is equal to the name of the program, then it brings the program to the front and closes the program. If not, then it opens a new program.

PyWin32 link

Stack Overflow get a list of every open window



来源:https://stackoverflow.com/questions/65717815/how-can-i-make-my-pyqt5-app-oney-one-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!