I'm writing a simple tray icon application.
But after adding a file dialog to a menu action, the whole application exits.
If I run the code on my Mac, it prints the file name, so it does not outright crash.
If I replace the call to getOpenFileName with a string, it continues to run.
import sys
from PySide import QtCore, QtGui
def share():
(filename, _) = QtGui.QFileDialog.getOpenFileName()
print(filename)
app = QtGui.QApplication(sys.argv)
icon = QtGui.QSystemTrayIcon(QtGui.QIcon('images/glyphicons-206-electricity.png'), app)
menu = QtGui.QMenu()
menu.addAction(QtGui.QAction("Share...", menu, triggered=share))
menu.addAction(QtGui.QAction("Quit", menu, triggered=app.quit))
icon.setContextMenu(menu)
icon.show()
app.exec_()
I'm using Mac OS X 10.10.1, Python 3.4.2, Qt 4.8.6 and PySide 1.2.2
By default Qt application implicitly quits when the last window is closed. To prevent it, you can use setQuitOnLastWindowClosed in QGuiApplication.
app = QtGui.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(false)
Related
Is there a way to change default menu title for native menu on MacOS for a simple pyqt5 app?
I'm trying to change "Python" menu entry on above image. Is there a way to rename it? Can I hide it somehow?
This code prints only "File":
menubar = self.menuBar()
for item in menubar.actions():
print(item.text())
menubar.setNativeMenuBar(False) doesn't help too (just move "File" into the MainWindow).
Update Sample app code:
from PyQt5 import QtCore, QtWidgets, uic
import sys
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
# QtCore.QCoreApplication.setApplicationName('QtFoo') # doesn't work
uic.loadUi('App.ui', self)
self.show()
# app = QtWidgets.QApplication(sys.argv)
app = QtWidgets.QApplication(["MyCoolApp"])
# app.setApplicationName("QtFoo") # doesn't work
# app.setApplicationDisplayName("Display Name")
window = Ui()
app.exec()
If you are not willing to package your app, you could create a temporary symbolic link to python as described here. This link can be used to execute python apps while displaying a custom name in the title bar:
go to the location of your app (e.g. my_app.py) file in the terminal
create symbolic link (replace location to your python interpreter, replace "BeautifulNaming" with desired name)
ln -s /Users/Christian/miniconda3/bin/python BeautifulNaming
call link to python with your script
./BeautifulNaming my_app.py
The "python" in the system menu bar appears because you run the script from the python, as soon as you package the application the title will disappear. For example the following code
# FileName PyQt5MenuProblem.py
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow
class AppTest(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle("My application")
self._createMenuBar()
def _createMenuBar(self):
self.menuBar = self.menuBar()
self.menuBar.setNativeMenuBar(False)
fileMenu = self.menuBar.addMenu("&File")
editMenu = self.menuBar.addMenu("&Edit")
if __name__== "__main__":
app = QApplication(sys.argv)
plotWindow = AppTest()
plotWindow.showMaximized()
sys.exit(app.exec_())
after packaging with
pyinstaller --onefile PyQt5MenuProblem.py
looks like
Key words: MacOS, User Interface, LSUIElement, pyinstaller, pyqt5
I guess that I am not closing my PyQT5 Window correctly. I am using spyder (3.3.5) which I have installed with anaconda, to program a pyqt5 program. I am using qt creator to design my ui file, which I load using the loadUi function in pyqt package. Everything works fine with the code, until I need to close it. I close the window via the close button (the x button in the top right). The window itself is closed, but it seems like the console (or shell) is stuck, and I can't give it further commands or to re run the program, without having to restart the kernel (to completely close my IDE and re opening it).
I have tried looking for solutions to the problem in the internet, but none seems to work for me. Including changing the IPython Console > Graphics to automatic.
Edit: Also Created an issure:
https://github.com/spyder-ide/spyder/issues/9991
import sys
from PyQt5 import QtWidgets,uic
from PyQt5.QtWidgets import QMainWindow
class Mic_Analysis(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui=uic.loadUi("qt_test.ui",self)
...
if __name__ == "__main__":
def run_app():
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app=QtWidgets.QApplication.instance()
mainWin=Mic_Analysis()
mainWin.show()
app.exec_()
run_app()
If someone have any suggestion I would be very happy to hear them.
For me, it helped to remove the 'app.exec_()' command.
But then it closes immediatly when running the code. To keep the window open, I needed to return the MainWindow instance to the global scope (or make it a global object). My code looks like this:
from PyQt5 import QtWidgets
import sys
def main():
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
main = MainWindow()
main.show()
return main
if __name__ == '__main__':
m = main()
Try adding :
app.setQuitOnLastWindowClosed(True)
to your main() function
def main():
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(True)
win = MainWindow()
win.show()
sys.exit(app.exec_())
Surprizinglly, for me this works well. No QApplication is needed.
It seems to work in another thread.
from PyQt5 import QtWidgets,uic
class Ui(QtWidgets.QDialog):
def __init__(self):
super().__init__()
uic.loadUi('./test.ui',self)
self.show()
w=Ui()
I want a QMainWindow which the user can close by: clicking the window close button, clicking an item in a menu, or using a keyboard shortcut. This is my solution:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (QAction, qApp, QApplication, QMainWindow)
class Window(QMainWindow):
'''A simple window'''
def __init__(self):
super().__init__()
self.make_gui()
def make_gui(self):
'''Create main GUI window'''
self.menuBar()
self.statusBar()
exitAction = QAction(QIcon(None), 'Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(qApp.quit)
fileMenu = self.menuBar().addMenu('Actions')
fileMenu.addAction(exitAction)
self.show()
if __name__ == '__main__':
APP = QApplication([])
GUI1 = Window()
sys.exit(APP.exec_())
When I run this code with python, this works every time, no matter which of the three actions is used to close the window. When I run it in IPython, one of these behaviors may happen:
Everything works as expected.
On exit, the window does not close.
On exit, the window does not close, and IPython exits.
On launch, warnings start to appear in the IPython shell. More appear
when interacting with the application. After these warnings first appear,
they will appear on every subsequent launch
until IPython is restarted.
Warning examples:
QWindowsContext::windowsProc: No Qt Window found for event 0x1c (WM_ACTIVATEAPP), hwnd=0x0xb0dd8.
QWindowsContext::windowsProc: No Qt Window found for event 0x2a3 (WM_MOUSELEAVE), hwnd=0x0xb0dd8.
QWindowsContext::windowsProc: No Qt Window found for event 0x24a (WM_POINTERLEAVE), hwnd=0x0xb0dd8.
QWindowsContext::windowsProc: No Qt Window found for event 0x1c (WM_ACTIVATEAPP), hwnd=0x0xb0dd8.
My environment: Python 3.7.2 64-bit on Windows 10.
I was going to write a PyQt application when i was setting the main screen up i realize while running that my main window is not showing up and i also have a warning saying parameters flag unfilled in PyCharm
import sys
from PyQt4 import QtGui
app = QtGui.Application(sys.argv)
main_window = QtGui.QWidget() # this where i am getting a warning
main_window.show()
Why this script opens file as soon as it is launched? No program is showed.
It is supposed to open file when the button is pressed.
If I remove widget.connect, then everything is ok. But the button does not working.
import sys
import os
from PyQt4 import QtGui, QtCore
# open file with os default program
def openFile(file):
if sys.platform == 'linux2':
subprocess.call(["xdg-open", file])
else:
os.startfile(file)
# pyQt
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
button = QtGui.QPushButton('open', widget)
widget.connect(button, QtCore.SIGNAL('clicked()'), openFile('C:\file.txt'))
widget.show()
sys.exit(app.exec_())
What is wrong with this widget.connect?
In your connect line openFile('C:\file.txt') is a call to the function openFile. When you connect a signal to a slot you're supposed to pass a callable, e.g. a function but you're passing the result of openFile.
As you want to hard code the parameter to openFile you need to create a new function which takes no arguments and when called calls openFile('C:\file.txt'). You can do this using a lambda expression, so your connect line becomes:
widget.connect(button, QtCore.SIGNAL('clicked()'), lambda: openFile('C:\file.txt'))