I found this question : Get screenshot on Windows with Python?
to take a screenshot of the full desktop:
import sys
from PyQt4.QtGui import QPixmap, QApplication
from datetime import datetime
date = datetime.now()
filename = date.strftime('%Y-%m-%d_%H-%M-%S.jpg')
app = QApplication(sys.argv)
QPixmap.grabWindow(QApplication.desktop().winId()).save(filename, 'jpg')
However, I would like to take a screenshot of an external window.
I have the hwnd of the window I want to take the screenshot off using win32gui.
According to the documentation for winId, the returned value is platform dependent.
So for Windows, it surely must (famous last words), return a hwnd and thus need no further conversion. If so, then try:
QPixmap.grabWindow(hwnd).save(filename, 'jpg')
(PS: I have actually tested this now on WinXP, and it works okay for me).
Related
I was following this tutorial on how to write a chess program in Python.
It uses the python-chess engine. The functions from that engine apparently return SVG data, that could be used to display a chessboard.
Code from the tutorial:
import chess
import chess.svg
from IPython.display import SVG
board = chess.Board()
SVG(chess.svg.board(board=board,size=400))
but when I run that code, all I see is a line in the terminal and no image.
<IPython.core.display.SVG object>
The tutorial makes a passing reference to Jupyter Notebooks and how they can be used to display SVG images. I have no experience with Jupyter Notebooks and even though I installed the package from pip and I dabbled a little into how to use it, I couldn't make much progress with regards to my original chessboard problem. But what I do have, is, experience with Qt development using C++ and since Qt has Python bindings, I decided to use those bindings.
Here is what I wrote:
import sys
import chess
import chess.svg
from PyQt5 import QtGui, QtSvg
from PyQt5.QtWidgets import QApplication
from IPython.display import SVG, display
app = QApplication(sys.argv);
board = chess.Board();
svgWidget = QtSvg.QSvgWidget(chess.svg.board(board=board, size=400));
#svgWidget.setGeometry(50,50,759,668)
svgWidget.show()
sys.exit(app.exec_())
A Qt window opens and shows nothing and in the terminal I see a lot of text - (apparently the SVG data is ending up in the console and not in the Qt window that is opening?).
I figured I have to install some SVG library under python so I installed drawSvg from pip. But it seems that library generates SVG images. And was of no use for me.
What is even more strange is, after seeing this SO question, I tried the following:
import sys
import chess
import chess.svg
from PyQt5 import QtGui, QtSvg
from PyQt5.QtWidgets import QApplication
from IPython.display import SVG, display
app = QApplication(sys.argv);
board = chess.Board();
svgWidget = QtSvg.QSvgWidget('d:\projects\python_chess\Zeichen_123.svg');
#svgWidget.setGeometry(50,50,759,668)
svgWidget.show()
sys.exit(app.exec_())
And it showed an image - an SVG image! What is the difference then between my case and this case?
Question: So my question is, what I am doing wrong in the case of the chessboard SVG data? Is the SVG data generated by the python-chess library not compatible with QtSvg?
I think you are getting confused by the scripting nature of Python. You say, you have experience with Qt development under C++. Wouldn't you create a main window widget there first and add to it your SVG widget within which you would call or load SVG data?
I would rewrite your code something like this.
import chess
import chess.svg
from PyQt5.QtSvg import QSvgWidget
from PyQt5.QtWidgets import QApplication, QWidget
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 1100, 1100)
self.widgetSvg = QSvgWidget(parent=self)
self.widgetSvg.setGeometry(10, 10, 1080, 1080)
self.chessboard = chess.Board()
self.chessboardSvg = chess.svg.board(self.chessboard).encode("UTF-8")
self.widgetSvg.load(self.chessboardSvg)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
EDIT
It would be even better if you would add a paint function to the MainWindow class. Because for sure in future, you would want to repaint your board image many times, whenever you would move a piece. So I would do something like this.
def paintEvent(self, event):
self.chessboardSvg = chess.svg.board(self.chessboard).encode("UTF-8")
self.widgetSvg.load(self.chessboardSvg)
this is how i display SVG image with python, no need third party to do that.
First i save SVG image on disk
second i just call it like os.Startfile("exemple.svg")
Exemple:
boardsvg = svg.board(board(fen), size=600, coordinates=True)
with open('temp.svg', 'w') as outputfile:
outputfile.write(boardsvg)
time.sleep(0.1)
os.startfile('temp.svg')
that's it !
I have created a program to read certain data from files on a drive, display results on a PyQt5 ui and accept corrections if any from user.
The program worked fine when run as a python file. However, when i converted it into a standalone exe using PyInstaller, it works fine upto the point where the pyqt5 gui needs to be launched. At this point, it stops throwing the following error :
qt.qpa.plugin: Could not load the Qt platform plugin "windows" in "" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: minimal, offscreen, windows.
I have read this, this and this but they dont resolve my issue.
The code of the gui is very big but here is the structure :
from PyQt5 import uic, QtWidgets
import sys
import os
#baseUIClass, baseUIWidget = uic.loadUiType('gui.ui')
baseUIClass, baseUIWidget = uic.loadUiType(r'C:\mypath\gui.ui')
class Ui(baseUIClass, baseUIWidget ):
def __init__(self, *args, **kwargs):
baseUIWidget.__init__(self, *args, **kwargs)
self.setupUi(self)
# More code to perform the desired actions
def run(input_variables):
app = QtWidgets.QApplication(sys.argv)
ui = Ui()
ui.show()
# More code to make the Ui perform desired actions
app.exec_()
return(output_variables)
The code was converted to a standalone exe with the following arguments :
pyinstaller --hiddenimport <hidden import> --onefile <python filename>
Would you know how to troubleshoot this please?
Thanks
I ran into the same error message with my compiled application.
To track down the issue, I first stripped down the app to its skeleton with minimal imports, which worked flawlessly when compiled. I then partially added back all imports of my "big" application until the error reappeared.
In the end (for me at least) pandas is the culprit:
Minimal reproducibale examples, this works:
from PyQt5 import QtWidgets
app = QtWidgets.QApplication([])
window = QtWidgets.QMainWindow()
window.show()
app.exec()
This (added pandas import in line 2) throws the error you describe:
from PyQt5 import QtWidgets
import pandas as pd # import after pyqt5
app = QtWidgets.QApplication([])
window = QtWidgets.QMainWindow()
window.show()
app.exec()
However, when first importing pandas and then importing PyQt 5, my compiled version works again:
import pandas as pd # import before pyqt5
from PyQt5 import QtWidgets
app = QtWidgets.QApplication([])
window = QtWidgets.QMainWindow()
window.show()
app.exec()
So, in my case the solution was to track down the "erroneous import", fiddle with the import order and get lucky (I am far too unexperienced to even try to understand why the import order leads to this error)
If you are not using pandas, maybe the whole "strip down to skeleton and start importing piece by piece"-approach will help you to further clarify the root cause of the error.
If you are using pandas and the order switch-up did not help, there is another thread which describes which tries to deal with pandas compiling issues.
I'm trying to load an image (a jpeg in that case from bytes, as it is downloaded from web content).
I've already consulted some posts (for ex. 1 & 2), but for some reason I can't reproduce those answers, though JPEG is a wholy implemented format by PyQT4 as stated in the doc.
Let's say the picture to load in a QImage is this one.
I'm first downloading picture using request (though I'm certain this isn't related to this particular problem - I put it here mainly for portability reasons...).
import requests
r = requests.get(href, stream=True)
pict_bytes = r.content
After that, I can check this image is 100% correct using PIL and io modules :
from PIL import Image
import io
image = Image.open(io.BytesIO(pict_bytes))
image.show()
image.save('./test.jpeg')
It gets confusing afterwards, while trying to convert bytes to QImage :
from PyQt4 import QtGui
qim = QtGui.QImage()
qim.loadFromData(pict_bytes)
qim.loadFromData(..) returns False whatever the picture I chose, which I can't understand regarding to the function's description in the doc. I also checked directly from the file :
with open('./test.jpeg', 'rb') as f:
content = f.read()
qim.loadFromData(content)
Is there something obvious that I missed, or is this some strange comportment of PyQt4 with python 3 ? I would be grateful for your insights...
EDIT
I'm starting to believe there is some bug here (responses are all coherent with what I had already tried, in one way or another). Something doesn't feel quite right with the PyQt4 QImage (as well as QPixmap I suspect).
I'm currently using Windows 10 (and used Windows 2008 Server at the office), Winpython 3.6 x64, with PyQt4 4.11.4 installed from the unofficial depositery of Christoph Gohlke.
Your example is not very clear, but you always have to do the verifications, for this you can use assert
import sys
import requests
from PyQt4.QtGui import QImage, QApplication, QLabel, QPixmap
app = QApplication(sys.argv)
href = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/OrteliusWorldMap.jpeg/800px-OrteliusWorldMap.jpeg"
r = requests.get(href, stream=True)
assert r.status_code == 200
img = QImage()
assert img.loadFromData(r.content)
w = QLabel()
w.setPixmap(QPixmap.fromImage(img))
w.show()
sys.exit(app.exec_())
Libraries:
requests 2.18.4
PyQt4 4.12.1
Python 2.7.14 and Python 3.6.4
It is always good to verify the appropriate formats, for this you must use:
print(QImageReader.supportedImageFormats())
I just installed Python3 (3.5.2) and Pyqt5 (5.8.2) and I am following this tutorial to learn and make a GUI: http://zetcode.com/gui/pyqt5/firstprograms/
I'm trying to run the 2nd example but program is returning an error (which also happened on the 1st one, but since it had no image i took no notice) which is the following:
QApplication: invalid style override passed, ignoring it.
No XVisualInfo for format QSurfaceFormat(version 2.0, options QFlags<QSurfaceFormat::FormatOption>(), depthBufferSize -1, redBufferSize 1, greenBufferSize 1, blueBufferSize 1, alphaBufferSize -1, stencilBufferSize -1, samples -1, swapBehavior QSurfaceFormat::SwapBehavior(SingleBuffer), swapInterval 1, profile QSurfaceFormat::OpenGLContextProfile(NoProfile))
Falling back to using screens root_visual.
What is the meaning of this? Am i missing some packages?
I installed pyqt first with this command:
sudo -H pip3 install PyQt5
but Python3 was not acknowledging its existence so i searched the apt ubuntu repos and installed with:
sudo apt install python3-PyQt5
I also tried to reference the image by full path /foo/bar/image.png and nothing
What is the problem?
EDIT #1
The code that i am using is from example 2:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
This example shows an icon
in the titlebar of the window.
author: Jan Bodnar
website: zetcode.com
last edited: January 2015
"""
import sys
import os
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon
base_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(base_dir)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('Icon')
self.setWindowIcon(QIcon('image.png'))
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
After your post i reinstalled all my packages. The error is slightly different but the result is the same:
python3 example_02.py
QApplication: invalid style override passed, ignoring it.
Screencapture:
Notice that you are having no icons at all for all applications, not just for the PyQt icon example. This is because by default, certain environments turn off the icons in the titlebar. You have to enable them.
For instance in Xfce Desktop Environment, we can use the xfce4-settings-editor tool. In Settings/Settings Editor select xfwm4.
Find the show_app_icon option and check it. Change a theme back and forth to see the changes; they are not visible right away.
After this, you will see the icon in the titlebar of the PyQt5 example.
As for the warning; it is a recent thing and it has to do something
with the incopatibilities between Qt and GTK theming. I have not found
a solution to remove the warning so far.
So first off, you have no errors in your code. That's more akin to a warning but not even. What the following line is telling you
QApplication: invalid style override passed, ignoring it
is that your style option is invalid. If that were an error your script wouldn't run at all.
What I see right off the bat is this, you never supply a path to your image.
Now if the image is in the same root directory as the script then it should recognize said image without a path. But if you're attempting to do what I think you are it wouldn't work like that anyway. I think you're trying to create a launcher icon as well as a title bar icon, which typically goes hand in hand.
It appears to me that you've added it to Atom as some form of resource file. In which case most Ide's create a path for that file. Sometimes it's a path, other times a local url. QT its self does both when working with the QT creator.
I've never used Atom so I can't tell you how that works.
What I can say is this. you're using Linux which means .ico files are useless. I told you before linux doesn't handle icon files the same way windows does. This is most likely your problem.
So I sugesst you take a look at this
https://askubuntu.com/questions/476981/how-do-i-make-a-desktop-icon-to-launch-a-program
After you read that take a look at this if you have to https://help.ubuntu.com/community/UnityLaunchersAndDesktopFiles#Using_a_text_editor
Both of those links explain how to create a launcher icon for your program.
The following link will explain how to set the icon on the menu bar (title bar) in your program.
PyQt4 set windows taskbar icon
I hope this helps you out!
I study PyQt5 from author who give this question,also I have this problem that my icons can't show,I try some ways to catch it,that's what I do,hope it works!
First, it's important that you should use absolute path of the icons,for example:
self.setWindowIcon(QIcon("F:/Workspace/PyQT5-Study/images/web.png"))
but this not a good idea,so you can use second way like this:
from PyQt5.QtCore import QFileInfo
# ...
def initUI(self):
# ...
root = QFileInfo(__file__).absolutePath()
self.setWindowIcon(QIcon(root+'/images/web.png'))
# or
# import os
# current_dir = os.path.dirname(os.path.realpath(__file__))
# self.setWindowIcon(QIcon(os.path.join(current_dir, 'images/web.png')))
# ...
Last, if your icons also can't show, you should check this icon, if it's a legal icon.
In short, the normal images are unlimited so more, they can store many images and transform easily, but the icons have sure size,color kind,and more important,the icons have transparency, that means you can see the background, they have frame(not always straight). So you can use the web online tools to transform your image and try again,that really help me!
Also you should check the icon's source format, ensure you never change it, like .jpg to .png,and other. This will produce problem!
Wish you can solve the problem!
On windows be sure to use a real .ico file and a full path
iconpath = os.path.join(os.getcwd(),'qtlearning','assets','python.ico')
self.setWindowIcon(QIcon(iconpath))
I faced the exact same problem.
First things first. There is no setWindowIcon() method under QWidget or QMainWindow classes, in fact. you should be trying to set the QIcon at the Application level as follows.
app = QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon('home.png'))
Second, the icon thus created using this code does not reflect on the title of the window, instead it will reflect as an application icon as shown in the image below. the home.png
" icon for Application" in Ubuntu and not the " icon over the Window Title"
Finally, the path does not really matter, it can be an absolute path or a relative path, the system will consider either.
i just provided the full path of icon as simple as that
I'm taking a screenshot using this line
screen = QPixmap.grabWindow(QApplication.desktop().winId())
but apparently that doesn't grab the full desktop if the user has several monitors.
Is there a way to grab the desktop of all monitors into a single image?
According to this blog, just add the x, y, width, and height to grab the full desktop.
Just need to loop over QApplication.screens() and grab them one at a time like so...
Note: using Python 3.8 and PyQt5
import os
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
for screen in QApplication.screens():
screen_path = os.path.expanduser(f"~/Desktop/{screen.name()}.jpg")
screen.grabWindow(0).save(screen_path, 'jpg')
# grabWindow(0) means full screen
# for area use following format; x=0, y=0, w=-1, h=-1