Simply stated I have a simple python application which generates random passwords. This application was originally written using Tkinter and currently works. I am trying to improve the GUI interface by employing PyQt5. My efforts, so far have resulted in an application that runs from within my IDE (Spyder) and can also be run by invoking python from the commandline with the fullpath of the python script.
It should be noted this works for the Tkinter as well as the PyQt implementation.
My next step was to define a shortcut on the desktop to execute this script and have a window appear allowing creation of a password. The shortcut for the Tkinter script performs as expected and results in a window appearing. The script for the PyQt5 based script does not work. The only differences between the scripts are the target files being invoked by the script. Also both script files are in the same directory. This is a side by side image of the shortcut properties.
Here is a very simplistic example of the Puqt5 code. This code exhibits the same characteristics as the original in that it runs in the IDE as well as directly from Python in the CMD window but will not execute from a shortcut icon.
"""
Created on Wed Sep 9 10:37:46 2020
"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget
def main():
app = QApplication(sys.argv)
w = QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Has anyone else had this type of problem or can anyone suggest an approach for determining what is the cause and solution
everything works on Windows 7
It work when invoked from a shortcut on Win &, hmm that's very strange.
I tested it on Windows 10 also works.
Thanks for the helpful suggestions as well as your efforts to test this problem on your own systems. I finally found an answer that satisfies my needs. I used pyinstaller to create an executable and then launch the executable from a desktop shortcut. This works, so I am considering my question closed.
Related
I can't manege to get the images or icons loaded. When I run the app in VScode the app works, just without the images. The Icons even have the space but there is nothing in the space where the icon is supposed to be. But running it in the Command Terminal works like it is supposed to.
Using the 'os.path. ...' method, and running it in VScode works every time. But why is it like that?
And, how will the os.path method affect the app when I run it alone? I.e. when I don't run it though the IDE or the Command Prompt (I'm planing to make it a stand alone app, just for fun).
A sample of code that works, the whole app is a bit bigger. (but same principles):
import sys
import os
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
current_directory = os.path.dirname(os.path.realpath(__file__))
widget = QLabel("")
image = QPixmap(os.path.join(current_directory, "39487.jpg" ))
widget.setPixmap(image)
widget.setScaledContents(True)
self.setCentralWidget(widget)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
Now why when i run this code, the images show. Yet when I remove the os.path method it does not work in the IDE. Running the second version in the Prompt does work though.
Why does the IDE load the code different than the Prompt?
The relative paths in python with respect to where the python command is executed, by default VScode launches python in the root folder of the project so it is not necessarily launched in the script folder, instead you use the command promt from the folder where There is the script and the icons, if you were to run from a higher folder you would also have the same problem. Due to the error that causes relative paths it is better to use absolute paths either explicitly or by constructing it using os.path.
Problem: The following program is keep starting itself every 5 seconds after making it executable using PyInstaller. It keeps running itself again and again. The code works fine without making if I run it directly using python.
I spent more than a day to fix this issue but no luck so far.
I tried it on Windows and Mac. Python version 3.7, PyInstaller Version 3.5 & 3.6.
PyInstaller Command:
pyinstaller --onefile generic_code_edit.py
Python Code:
import logging
logging.basicConfig(level=logging.DEBUG)
import sys
from pyqode.qt import QtWidgets
from pyqode.core.widgets import GenericCodeEdit
def main():
app = QtWidgets.QApplication(sys.argv)
# create editor and window
window = QtWidgets.QMainWindow()
editor = GenericCodeEdit()
# open a file
editor.file.open(__file__)
window.setCentralWidget(editor)
# run
window.show()
app.exec_()
editor.file.close()
if __name__ == "__main__":
main()
The Debug log:
DEBUG:pyqode.core.cache:getting encoding for generic_code_edit.py
DEBUG:pyqode.core.managers.file:mimetype detected: text/x-python
DEBUG:pyqode.core.managers.file:file open: generic_code_edit.py
ERROR:pyqode.backend:DEBUG:pyqode.qt:importing PyQt5
ERROR:pyqode.backend:DEBUG:pyqode.qt:imported PyQt5
ERROR:pyqode.backend:INFO:pyqode.qt:using pyqt5
ERROR:pyqode.backend:DEBUG:pyqode.core.cache:getting encoding for
generic_code_edit.py
ERROR:pyqode.backend:DEBUG:pyqode.core.managers.file:mimetype
detected: text/x-python
ERROR:pyqode.backend:DEBUG:pyqode.core.managers.file:file open:
generic_code_edit.py
Update:
Finally I found the problem. It is related to the autocomplete feature. This feature does not load perfectly with pyinstaller and cause a new start. Although I could not be able to make it working with pyinstaller, but I have been able to stop these feature to solve the starting loop issue.
I have passed an empty server file as backend to solve the issue. But still I want to make the Autocomplete working with pyinstaller.
editor = GenericCodeEdit(None, 'empty_file.py')
When I execute my PyQt script in Spyder, it doesn't seem to do anything except clearing variables. When I execute it again, it works as expected.
As you can see below, I cannot reduce the code any further, but the problem remains unchanged.
Is this the expected behaviour? What am I doing wrong?
import sys
from PyQt5 import QtWidgets
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
window.show()
app.exec_()
In detail:
I open Spyder.
I execute the script. A window opens and I terminate by closing the window (expected behaviour).
I execute the script. After a few seconds, the console returns. Nothing seems to have happened except that the variables are gone (unexpected behaviour).
Repeat from 2...
The problem isn't so much that the variables are being cleared, but rather that the PyQt application cannot be run repeatedly within Spyder. It's a common issue that is addressed in the Spyder Wiki: How to run PyQt applications within Spyder.
It seems to be tied to the fact that Spyder is itself a Qt application. Specifically, the Wiki entry has this by way of an explanation:
The most common problem when running a PyQt application multiple times inside Spyder is that a QApplication instance remains in the namespace of the IPython console kernel after the first run. In other words, when you try to re-run your application, you already have a QApplication instance initialized.
The work-around is to make sure that the QApplication instance goes out of scope when your script exits, i.e., create it within the scope of a function. Using the simple example from above, it just comes down to this:
import sys
from PyQt5 import QtWidgets
def main():
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
window.show()
app.exec_()
main()
I have made a GUI for my application. All scripts are in Python (2.7) and the GUI is created with Tkinter. I work on Linux, but I needed this app to be executable on Windows. So I've used py2exe, to create an executable. After a while it is working almost perfectly, but I have the following problem:
Somewhere in the application, I need to call external programs, namely ImageMagick and LaTeX. I use the commands convert, pdflatex, simply by importing os module and running os.system(build), where build = 'convert page.pdf page.gif'etc. When those commands are called from the *.exe application the console pops up (meaning a console window opens up for a split of a second and closes again). Is there a way, to prevent this behaviour?
It does not interrupt the application, but it is ugly and not a desired behaviour.
[Note] I chose not to add any samples, since there are lots of files and other content, which, I think, is not related to the problem. I could however try to post minimal (not)working example. But maybe it is not needed.
Thanks!
import subprocess
subprocess.Popen("application.exe", shell = True)
Under OS X, using the zetcode example for menubar, if one runs this from the command line:
$ python menubar.py
the application starts, but at the bottom of the window stack. This can be fixed by adding
self.raise_()
to the code.
However, the application is still not immediately active. The terminal application menubar still shows, not the pyqt menubar.
The only way to get the pyqt menubar to show is to switch away from the pyqt application or terminal application and back again.
Is this expected behavior? Is there anyway to fix this so the pyqt application immediately becomes active, i.e. the pyqt menubar immediately shows upon execution.
Versions: OS X 10.9, Qt 4.8.6, PyQt 4.11.3, SIP 4.16.5, python 2.7.8
The identical problem affects some apps bundled by PyInstaller. The following blog post discusses the fix, however the fix is in terms of the PyInstaller boot loader, which would not be directly relevant to your code, but it might be informative.
http://dvitonis.net/blog/2015/01/07/menu-bar-not-visible-when-building-pyqt-app-bundle-pyinstaller-mac-osx-mavericks-yosemite/
The problem relates to use of TransformProcessType() which is discussed in this linked article:
http://www.sheepsystems.com/developers_blog/TPT-show-menu.html
I do not know what Qt method would relate to the Mac OS TransformProcessType() but you might experiment with calling QWidget.setWindowState() in your mainwindow's __init__().
I was experiencing this behavior, but only when I launch with a --nosplash option to skip the splash screen in my application.
That gave me a hint, and I developed this workaround using a 'dummy' splash screen:
widget = QtGui.QMainWindow() # or whatever your main window class is
dummy = QtGui.QSplashScreen()
dummy.show()
dummy.finish(widget)
widget.show()
widget.raise_()