snapshoot of widget pyqt5 - python

Hi every one I am traying to get a snapshoot of a widget using pyqt5 I am using this code but I can't create an object of the class QScreen
I get an error :
PyQt5.QtGui.QScreen cannot be instantiated or sub-classed
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap,QScreen
from PyQt5.QtWidgets import QApplication
from datetime import datetime
date = datetime.now()
filename = date.strftime('%Y-%m-%d_%H-%M-%S.jpg')
app = QApplication(sys.argv)
sc=QtGui.QScreen()
sc.grabWindow(QApplication.desktop().winId()).save(filename, 'jpg')

You can get a reference to the screen with :
sc = app.screens()[0]
This method returns a list of screens, i assume you want the first (with index [0])

Related

To turn back from 2nd window to 1st window (different files)

An application is being created with a large number of different windows.
The bottom line is that I am trying to get from the main window (by clicking button) to the client window. There is "Back" button in the clients window, which should return the user to the main window.
Both codes are in different files. The problem occurs at the stage of pressing the "Customers" button (Process finished with exit code -1073740791 (0xC0000409)).
Moreover, if everything is in one file, then everything works fine. But only here the program is not planned to be small, so I don't want to put a huge code in one file.
Welcome_Screen.py
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QDialog, QApplication
import Customers
class WelcomeScreen(QDialog):
def __init__(self):
super(WelcomeScreen, self).__init__()
loadUi('screens/welcomescreen.ui', self)
self.CustomerData.clicked.connect(self.go_to_CustomerData)
def go_to_CustomerData(self):
client = Customers.CustomerScreen()
widget.addWidget(client)
widget.setCurrentIndex(widget.currentIndex() + 1)
# main
app = QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon('Icons/MainIcon.png'))
app.setApplicationDisplayName('Bonus Program')
welcome = WelcomeScreen()
widget = QtWidgets.QStackedWidget()
widget.addWidget(welcome)
widget.setFixedWidth(600)
widget.setFixedHeight(475)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Exiting")
Customers.py
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QDialog, QApplication, QWidget, QMessageBox, QTableWidget, QTableWidgetItem, QTableView
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel, QSqlQuery
from PyQt5.QtGui import QIcon
import Welcome_Screen as ws
import sqlite3
db_path = 'Bonus_db.sqlite'
class CustomerScreen(QDialog):
def __init__(self):
super(CustomerScreen, self).__init__()
loadUi("screens/customerscreen.ui", self)
self.back_btn.setIcon(QIcon('Icons/Back.png'))
self.back_btn.setIconSize(QtCore.QSize(45, 60))
self.back_btn.clicked.connect(self.go_Back_to_WelcomeScreen)
# self.SoldTos.clicked.connect(self.gotoSoldTos)
def go_Back_to_WelcomeScreen(self):
welcome = ws.WelcomeScreen()
ws.widget.addWidget(welcome)
ws.widget.setCurrentIndex(ws.widget.currentIndex() + 1)
Customer_app = QApplication(sys.argv)
cust_window = CustomerScreen()
cust_window.show()
sys.exit(Customer_app.exec_())

Triggering action when QTableView selection is changed [duplicate]

I have a problem with my PyQt button action. I would like to send a String with the Function but I got this Error:
TypeError: argument 1 has unexpected type 'NoneType'
import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QAction
from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtGui import *
from PyQt5.uic import *
app = QApplication(sys.argv)
cocktail = loadUi('create.ui')
def mixCocktail(str):
cocktail.show()
cocktail.showFullScreen()
cocktail.lbl_header.setText(str)
widget = loadUi('drinkmixer.ui')
widget.btn_ckt1.clicked.connect(mixCocktail("string"))
widget.show()
sys.exit(app.exec_())
As suggested by user3030010 and ekhumoro it expects a callable function. In which case you should replace that argument with lambda: mixCocktail("string")
AND ALSO don't use str it's a python built-in datatype I have replaced it with _str
import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QAction
from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtGui import *
from PyQt5.uic import *
app = QApplication(sys.argv)
cocktail = loadUi('create.ui')
def mixCocktail(_str):
cocktail.show()
cocktail.showFullScreen()
cocktail.lbl_header.setText(_str)
widget = loadUi('drinkmixer.ui')
widget.btn_ckt1.clicked.connect(lambda: mixCocktail("string"))
widget.show()
sys.exit(app.exec_())
More about lambda functions: What is a lambda (function)?
instead of this
widget.btn_ckt1.clicked.connect(mixCocktail("string"))
write
widget.btn_ckt1.clicked.connect(lambda:mixCocktail("string"))

Problem of how to link input from a form to a variable PyQt5

I am new on Python and PyQt5.
I used Qt Design to create a form for input.
I am able to print the 3 data that the user input but I don't know how to store them in a variable so that I can use them in the main program.
import PyQt5 as pq
import sys
from PyQt5 import QtWidgets, uic, QtGui
def Assigned(self):
Nx=call.Nx11.text()
Ny=call.Ny11.text()
Nxy=call.Nxy11.text()
Mx=call.Mx11.text()
My=call.My11.text()
Mxy=call.Mxy11.text()
print(Nx)
print(Ny)
print(Nxy)
return(Nx)
return(Ny)
return(Nxy)
app=QtWidgets.QApplication([])
call=pq.uic.loadUi("InputLoad.ui")
call.OKbutton.clicked.connect(Assigned)
call.show()
app.exec_()
Thank you in advance for any help.
Fab
Create a global variable and store them
import PyQt5 as pq
import sys
from PyQt5 import QtWidgets, uic, QtGui
Nx = None
Ny = None
Nxy = None
def Assigned(self):
global Nx, Ny, Nxy
Nx=call.Nx11.text()
Ny=call.Ny11.text()
Nxy=call.Nxy11.text()
Mx=call.Mx11.text()
My=call.My11.text()
Mxy=call.Mxy11.text()
print(Nx)
print(Ny)
print(Nxy)
app=QtWidgets.QApplication([])
call=pq.uic.loadUi("InputLoad.ui")
call.OKbutton.clicked.connect(Assigned)
call.show()
app.exec_()

'QPixmap' has no attribute 'grabWindow'

I am trying convert my code from PyQt4 to PyQt5 but I am getting errors.
import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import 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')
Traceback (most recent call last):
File "C:\Python34\Projects\name.py", line 9, in <module>
QPixmap.grabWindow(QApplication.desktop().winId()).save(filename, 'jpg')
AttributeError: type object 'QPixmap' has no attribute 'grabWindow'
You should use QScreen::grabWindow() instead. QPixmap::grabWindow() is deprecated in Qt 5.0 because:
there might be platform plugins in which window system identifiers (WId) are local to a screen.
grabWindow method is now available in QScreen class.
You need to create QScreen object, initialize it with ex. QtGuiApplication.primaryScreen() and then grab the screen
screen.grabWindow(QApplication.desktop().winId())
Full example for PyQt5
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QPixmap, QScreen
from datetime import datetime
date = datetime.now()
filename = date.strftime('%Y-%m-%d_%H-%M-%S.jpg')
app = QApplication(sys.argv)
QScreen.grabWindow(app.primaryScreen(),
QApplication.desktop().winId()).save(filename, 'png')

Screenshot of a window using python

I'm trying to take a screenshot of the curent window using a python script on linux.
I curently have a script which takes a screenshot of the entire screen:
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')
But a would like to have only the selected window. I know that the problem comes from grabWindow. But I don't know how to resolve it.
simply replace
QApplication.desktop()
with the widget you want to take the screenshot of.
import sys
from PyQt4.QtGui import *
from datetime import datetime
date = datetime.now()
filename = date.strftime('%Y-%m-%d_%H-%M-%S.jpg')
app = QApplication(sys.argv)
widget = QWidget()
# set up the QWidget...
widget.setLayout(QVBoxLayout())
label = QLabel()
widget.layout().addWidget(label)
def shoot():
p = QPixmap.grabWindow(widget.winId())
p.save(filename, 'jpg')
label.setPixmap(p) # just for fun :)
print "shot taken"
widget.layout().addWidget(QPushButton('take screenshot', clicked=shoot))
widget.show()
app.exec_()
Since Qt5, grabWindow and grabWidget are obsolete (see Obsolete Members for QPixmap)
Instead, you can use QWidget.grab()
p=widget.grab()
Alternatively, instead of
p = QPixmap.grabWindow(widget.winId())
you can also use
p = QPixmap.grabWidget(widget)
PyQt5 update
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QPixmap, QScreen
from datetime import datetime
date = datetime.now()
filename = date.strftime('%Y-%m-%d_%H-%M-%S.jpg')
app = QApplication(sys.argv)
QScreen.grabWindow(app.primaryScreen(),
QApplication.desktop().winId()).save(filename, 'png')

Categories

Resources