PyQt 5 and 4k screen - python

I'm trying to migrate an existing PyQt5 app to high-dpi for window 10.
The documentation of Qt5 itself speak about high-dpi scaling here:
http://doc.qt.io/qt-5/highdpi.html
QT_AUTO_SCREEN_SCALE_FACTOR to "1".
But i can't adapt this in python code :/
Any idea ?

Make sure you're on Qt 5.6+, skim the docs to be aware, then change your app init code to include one line before it and one line after it (written in Python) but it would be similar in C++:
os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
qapp = QApplication(sys.argv)
qapp.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)

Related

PYQT Display stops displaying changes periodically, fixes when changing element

I am having a small issue with PYQT that I cannot seem to find an answer for. I have made several apps using PYQT that do a multitude of functions but they all seem to have this one similar issue. When I leave my machine (windows 10) idle for a bit (not moving mouse/keyboard), the display will appear to 'freeze' but it is not actual frozen, just the display. The way to fix this is to change ANY element (increase spinbox value, minimize/maximize screen, etc).
This is particularly a problem with a video app I am working on where a camera is displaying video and suddenly it looks like the app is frozen until I 'fix' it. I am not sure if its a PYQT thing or perhaps windows itself. I will post relevant code below:
I can post more as needed but this is how I start my app:
app = QtWidgets.QApplication(sys.argv)
window = Ui()
widget = QtWidgets.QStackedWidget()
widget.addWidget(window)
widget.showMaximized()
app.exec_()
window.stop_threads()
cv2.destroyAllWindows()

Python Kernel crashes after closing an PyQt4 Gui Application

Ok here is my problem:
I want to create a PyQt4 Gui, which can be executed from a python console (tested with IDLE, Spyder Console and IPython Console) and then allows the user to change and view variables. After closing the app the user should be able to do further work with the variables in the console. But by closing the Gui the Kernel crashes and it is not possible to make any new input to the console.
I'm working with Python 2.7 and PyQt4. I am using the following code to start an close the application:
app=QtGui.QApplication(sys.argv)
MainApp=plottest()
MainApp.show()
sys.exit(app.exec_())
The easy solution here https://www.reddit.com/r/learnpython/comments/45h05k/solved_kernel_crashing_when_closing_gui_spyder/
only put
if __name__ == "__main__":
app=0 #This is the solution
app = QtGui.QApplication(sys.argv)
MainApp = Dice_Roller()
MainApp.show()
sys.exit(app.exec_())
What you need to do is:
Check that there isn't already a QApplication instance when trying to create a new one
Ensure that the QApplication object is deleted after it's been closed
(See simple IPython example raises exception on sys.exit())
# Check if there's a pre-existing QApplication instance
# If there is, use it. If there isn't, create a new one.
app = QtGui.QApplication.instance()
if not app:
app = QtGui.QApplication(sys.argv)
# Ensure that the app is deleted when we close it
app.aboutToQuit.connect(app.deleteLater)
# Execute the application
MainApp = plottest()
MainApp.show()
sys.exit(app.exec_())
Using this code you can rerun the application as many times as you want in IPython, or anywhere else. Every time you close your Qt application, the app object will be deleted in python.
I do not think you mean a kernel crash. Rather, I think you are talking about exiting the python console. This is caused by sys.exit(app.exec_()). For example try (for example in spyder) the following two codes:
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.setWindowTitle('simple')
widget.show()
#sys.exit(app.exec_())
Here you should get an empty window and the python console will stay alive. The second one, where sys.exit(app.exec_()) is included, will exit the python console at the end and the window disappears:
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.setWindowTitle('simple')
widget.show()
sys.exit(app.exec_())
I hope this helps.
I still don't know the issue and solution. None of the above worked for me. Currently I am coding in the spyder GUI and running on "cmd" by giving 'python' command. In CMD, it is working fine.
Update the solution if I get it. :-)
I think that your problem is that python console closes (its not kernel crash). For example in Spyder python icon on top of console window becomes grey and you cant do anything besides running another console.
Anyway, you should write app.exec() instead of sys.exit(app.exec()).
I believe sys.exit(app.exec()) passes exit code to console and closes it. Using simple app.exec() won't close console.
Concluding your code should look like:
app=QtGui.QApplication(sys.argv)
MainApp=plottest()
MainApp.show()
app.exec_()
I hope it helps.
hdunn's answer worked for me. Have deleted sys.exit() to avoid exiting the application totally. Performing the first step alone, as suggested by others in github forum, solved part of the problem but window never showed up, nor any error message. Performing the second part solved everything like a charm! Thans, hdunn!
# Check if there's a pre-existing QApplication instance
# If there is, use it. If there isn't, create a new one.
app = QtGui.QApplication.instance()
if not app:
app = QtGui.QApplication(sys.argv)
# Ensure that the app is deleted when we close it
app.aboutToQuit.connect(app.deleteLater)
# Execute the application
MainApp = plottest()
MainApp.show()
app.exec_()

pyqt Playing movie files in phonon player QT4.9

I'm definitely in need of your help guys.. As in really.
My laptop has been stolen and I didn't have a backup of my
pyqt phonon video player that I made a year ago. I forgot how and what to do to recreate it.
I only know some key things to do for it to work. So please help me out.
From what i can remember I need to
Set backend capabilities (set phonon backend to windows media player?)
Install the required codecs (which i dont have a copy of)
Code the program (and sadly I forgot how to play a video)
If there's someone out there who have a working sample python videoplayer,
can you please share it with me?
I'm trying it right now and my sample doesn't work at all
from PyQt4.phonon import Phonon
media_source = phonon.Phonon.MediaSource("C:\\Sample.avi")
self.ui.videoPlayer.load(media_source)
self.ui.videoPlayer.play()
Please help me. And thank you very much to you guys.
I'm using python 2.6 and qt version 4.9. Now I'm coding on a virtual box windows XP
EDIT:
got a following sample with this problem but..
having this error when loading a file.
"The Operation Cannot Be Performed Because the Pins Are Not Connected"
This maybe because I'm using a virtual box in Ubuntu?
Don't forget to show() the videoplayer. For the rest, in my experience Phonon often has trouble finding the codecs needed to play videos on Windows. Installing K-Lite codec pack might work in that situation.
Here's a self-contained example that does work for me (Windows Vista32, Python 2.6.5, PyQt 4.7.3).
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.phonon import Phonon
app = QtGui.QApplication(sys.argv)
vp = Phonon.VideoPlayer()
media = Phonon.MediaSource('C:\\video.mp4')
vp.load(media)
vp.play()
vp.show()
sys.exit(app.exec_())
Edit:
Multiple people have recently commented that the above code no longer gives the desired behavior. I haven't worked with PyQt in ages, but I suspect that one of the updates might have changed Phonon functionality.
According to the commenters, vp.show() now needs to be called before Phonon.MediaSource(), i.e.:
...
vp = Phonon.VideoPlayer()
vp.show()
media = Phonon.MediaSource('C:\\video.mp4')
vp.load(media)
vp.play()
sys.exit(app.exec_())

PyQt4 Windows - Toolbar Actions don't work. How can I fix them?

I have made a program that has a Toolbar with a few actions on it in Linux. The Toolbar and its actions work perfectly on a Linux OS, but when I run the code on a Windows machine, the actions just don't work. Everything looks the same as on Linux(yes I have PyQt4 installed on the Windows machine), even the toolbar, but when I click an item on the toolbar it just doesn't execute the given handler. I tried and made a basic PyQt4 program with a toolbar only to test if the bug is because of my program, but the results are the same. Here's the basic example program which doesn't work.
from PyQt4 import QtGui, QtCore
import sys
def aTest(obj = None):
print "If this appears, the action works."
app = QtGui.QApplication(sys.argv)
w = QtGui.QMainWindow()
toolBar = w.addToolBar("toolbar")
action = QtGui.QAction(QtGui.QIcon("path to icon here"), "testAction", w)
w.connect(action, QtCore.SIGNAL("activated()"), aTest)
toolBar.addAction(action)
w.show()
app.exec_()
This code works on Linux without a problem, but on Windows the actions just don't react when activated. Ignore the typos in the code.
OS: Windows 7 Ultimate 32-bit (doesn't work on XP either)
Python version: Python 2.7
PyQt4 version: PyQt-Py2.7-x86-gpl-4.8.4-1
I hope someone out there can help me. I thank you all for your interest and help!
Have a nice day!
It's odd that it works in Linux, because I can only get it work on Windows by changing
w.connect(action, QtCore.SIGNAL("activated()"), aTest)
to
w.connect(action, QtCore.SIGNAL("triggered()"), aTest)
The QAction class reference only lists changed(), hovered(), toggled() and triggered() as valid signals. Unfortunately I don't have a Linux box handy to test things out further.

Cross-platform Python GUI suitable for taskbar (Win) and menubar (mac) functionality?

I am fairly new to Python programming, and completely new to cross-platform GUI building (only previous GUI experience is through visual basic and Java).
I've written some python code to screen-scrape data from a website, and now I want to build a GUI that will reside in the Mac OS X menubar, and in Window's task bar (i.e., the system tray).
The most useful general page on cross-plaform Python GUIs for me was this one (despite its name indication Window GUIs). And some stackoverflow questions came in useful as well (especially this one, and the accepted answer of this one about splitting up the GUI and cli code).
I think I will go for either wxPython or QT because I want the GUI to look as native as possible.
However, as I've said the fairly simple GUI will mainly live in the taskbar/menubar.
Should this influence my decision?
Here's an example for PyQt. This works for me on MacOS X; I haven't tried it on other platforms. Note that the QSystemTrayIcon class will raise exceptions if it doesn't have an icon – I grabbed the RSS feed svg from Wiki commons for my icon.svg (but you can give QIcon a PNG directly and not mess around with QtSvg).
import PyQt4
from PyQt4 import QtCore, QtGui, QtSvg
app = QtGui.QApplication([])
i = QtGui.QSystemTrayIcon()
m = QtGui.QMenu()
def quitCB():
QtGui.QApplication.quit()
def aboutToShowCB():
print 'about to show'
m.addAction('Quit', quitCB)
QtCore.QObject.connect(m, QtCore.SIGNAL('aboutToShow()'), aboutToShowCB)
i.setContextMenu(m)
svg = QtSvg.QSvgRenderer('icon.svg')
if not svg.isValid():
raise RuntimeError('bad SVG')
pm = QtGui.QPixmap(16, 16)
painter = QtGui.QPainter(pm)
svg.render(painter)
icon = QtGui.QIcon(pm)
i.setIcon(icon)
i.show()
app.exec_()
del painter, pm, svg # avoid the paint device getting
del i, icon # deleted before the painter
del app
See this related SO answer on how to accomplish Windows system tray/OS X menu bar functionality in wxPython.

Categories

Resources