Current Screen Size in Python3 with PyQt5 - qt

Is there an alternative in Qt5 python3 to the following code :
https://askubuntu.com/questions/153549/how-to-detect-a-computers-physical-screen-size-in-gtk
from gi.repository import Gdk
s = Gdk.Screen.get_default()
print(s.get_width(), s.get_height())

You can get the primary screen from the QApplication, which returns a QScreen object giving access to many useful properties:
import sys
from PyQt5 import QtWidgets
app = QtWidgets.QApplication(sys.argv)
screen = app.primaryScreen()
print('Screen: %s' % screen.name())
size = screen.size()
print('Size: %d x %d' % (size.width(), size.height()))
rect = screen.availableGeometry()
print('Available: %d x %d' % (rect.width(), rect.height()))
Note that the primaryScreen method is static, so if you've already created an instance of QApplication elsewhere in your application, can easily get a QScreen object later on like this:
screen = QApplication.primaryScreen()

The following python3 code allows to get screen size but is there an alternative to
QtWidgets.QDesktopWidget().screenGeometry(-1) method :
import sys
from PyQt5 import QtWidgets
def main():
"""
allow you to get size of your current screen
-1 is to precise that it is the current screen
"""
sizeObject = QtWidgets.QDesktopWidget().screenGeometry(-1)
print(" Screen size : " + str(sizeObject.height()) + "x" + str(sizeObject.width()))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main()
sys.exit(app.exec_())

The value for screenGeometry() is the display you take a look at.
0 is the main screen, 1, 2 indicates further monitors installed.
To list all availible displays:
def screen_resolutions():
for displayNr in range(QtWidgets.QDesktopWidget().screenCount()):
sizeObject = QtWidgets.QDesktopWidget().screenGeometry(displayNr)
print("Display: " + str(displayNr) + " Screen size : " + str(sizeObject.height()) + "x" + str(sizeObject.width()))

from PyQt5.QtWidgets import QApplication, QWidget
self.desktop = QApplication.desktop()
self.screenRect = self.desktop.screenGeometry()
self.height = self.screenRect.height()
self.width = self.screenRect.width()
You can see here - https://www.programmersought.com/article/58831562998/

Screen information is available from the QApplication object as noted by ekhumoro. However, there should be only one global instance of QApplication. It is often set in the main script of a PyQt Gui application:
import sys
from PyQt5.QtWidgets import QApplication
def main():
...
app = QApplication(sys.argv)
window = MainWindow()
window.show
sys.exit(app.exec_())
That's probably not where you want to work with screen properties.
As pointed out by Omkar76, to access the global instance from elsewhere (e.g. in MainWindow()) use its instance() function. You can then use its primaryScreen property to get access to the many useful pieces of information available from the QScreen object:
from PyQt5.QtWidgets import QApplication, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
# Window dimensions
app = QApplication.instance()
screen = app.primaryScreen()
geometry = screen.availableGeometry()
self.setFixedSize(geometry.width() * 0.8, geometry.height() * 0.7)
QDesktopWidget is obsolete and so should be avoided.

i honestly don't know why it's always such a hustle to find pyqt5 answers,but anyway i think this is what you were looking for .
print(self.frameSize())

Related

How do I prevent double valueChanged events when I press the arrows in a QSpinbox?

Could be a either a Qt bug, or a bug in my app, I am hoping someone can help me diagnose this.
I am building a PyQt5 application with an interface built using qtdesigner and pyuic5.
I have to QSpinBox widgets in my window, which I connect to two methods like this:
self.img1IndexBox.valueChanged.connect(self.changeImage1)
self.img2IndexBox.valueChanged.connect(self.changeImage2)
Everything seems to work fine in the application, except for one thing: If I scroll the mouse over the spinbox, I can make it increment the value. If I change the value with text, all works fine. If I use keyboard arrows on the spinbox, it works fine. But if I click on either the up or down arrows from the spinbox, I get get two changeValue events, a double increment. Like I clicked twice. In fact, it even looks from the animation that it is creating an event for the downpress, and another when the button goes back up.
Could this be just a library bug, or what could be causing this in my program? How could I debug this?
You might be able to prevent that double thing by setting spinbox enable to false.
then make it enable to true after processing large data.
Upon clicking the arrow up/down
on_valuechanged
ui->spinbox->setEnabled(false);
then set to true before the function on_valuechanged ends.
ui->spinbox->setEnabled(true);
Apparently the problem is the event is triggering a very long routine, this delays the "button release" event, and it is enough time to make the system think the user is actually holding the button, generating more events... But I would still be interested in learning what would be a good walk-around. Would there be a nice pyqt-onic way to start a thread for that method?
http://www.qtcentre.org/archive/index.php/t-43078.html
Instead of using valueChanged.connect use editingFinished.connect it will make sure the function is called only after value is provided.
PyQt
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QDoubleSpinBox, QApplication, QMainWindow, QWidget, QVBoxLayout, QLabel, QSpinBox
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
# def valueChanged(self, value):
# print(value)
def valueChanged(self):
print(f"Value changed new value is : {self.spinBox.value()}")
def initUI(self):
self.setGeometry(0, 0, 200, 100)
self.layout = QVBoxLayout()
self.spinBox = QSpinBox()
self.spinBox.setAlignment(Qt.AlignCenter)
self.spinBox.setRange(0, 1000)
# self.spinBox.valueChanged.connect(self.valueChanged)
self.spinBox.editingFinished.connect(self.valueChanged)
self.layout.addWidget(self.spinBox)
self.setLayout(self.layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec())
PySide
import sys
from PySide6.QtWidgets import QDoubleSpinBox, QApplication, QMainWindow, QWidget, QVBoxLayout, QLabel, QSpinBox
from PySide6.QtGui import Qt
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
# def valueChanged(self, value):
# print(value)
def valueChanged(self):
print(f"Value changed new value is : {self.spinBox.value()}")
def initUI(self):
self.setGeometry(0, 0, 200, 100)
self.layout = QVBoxLayout()
self.spinBox = QSpinBox()
self.spinBox.setAlignment(Qt.AlignCenter)
self.spinBox.setRange(0, 1000)
# self.spinBox.valueChanged.connect(self.valueChanged)
self.spinBox.editingFinished.connect(self.valueChanged)
self.layout.addWidget(self.spinBox)
self.setLayout(self.layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec())

QWebEngineView, post KeyEvents inside the View

I have my own "Virtual Keyboard". I already got to transform buttons clicked into KeyEvents and deliver it to QTextEdit and so on. My problem now is that I want to do the same for writable areas inside a QWebEngineView.
For example, I use my keyboard to edit my QLineEdit, and request a website. DONE
Let's say I requested google. Now I have the Google website right in front of me. I need to send KeyEvents from my Keyboard to it's search box.(Box that is inside my QWebEngineView.
Let's now point a few points:
I am using PyQt5
As I've read, the API says me that it's parent should consume the KeyEvent to the corect place. here
This snippet says "...like it was possible with QtWebKit."
I've seen now that there is no more QtWebKit, and so Chromium instead.(Maybe that's the reason I'm not getting to post these events)
This is what I have for example to simulate KeyEvents to my QEditText and whatever..
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtCore import QEvent
from PyQt5.QtCore import QSize
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtGui import QKeyEvent
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QPushButton
class KeyboardKey(QPushButton):
__path = ""
__size = [30, 30]
__style = ""
__icon_on = ""
__icon_off = ""
__auto_repeat = True
__receiver = None
__key = None
__str_key = None
def __init__(self, style, str_icon_on, str_icon_off, auto_repeat, size, receiver, key, str_key):
super(KeyboardKey, self).__init__()
self.__size = size
self.__style = style
self.__icon_on = str_icon_on
self.__icon_off = str_icon_off
self.__auto_repeat = auto_repeat
self.__receiver = receiver
self.__key = key
self.__str_key = str_key
self.set_up_button(style, str_icon_on, str_icon_off, auto_repeat, size, receiver, key, str_key)
def set_up_button(self, style, str_icon_on, str_icon_off, auto_repeat, size, receiver, key, str_key):
self.__size = size
self.__style = style
self.__icon_on = str_icon_on
self.__icon_off = str_icon_off
self.__auto_repeat = auto_repeat
self.__receiver = receiver
self.__key = key
self.__str_key = str_key
self.setText(str_key)
self.setFixedSize(size[0], size[1])
self.setStyleSheet(style)
self.setIconSize(QSize(size[0], size[1]))
self.setIcon(QIcon(self.__path + str_icon_off + ".png"))
self.setAutoRepeat(auto_repeat)
pixmap = QPixmap(self.__path + str_icon_off + ".png")
self.setMask(pixmap.mask())
self.pressed.connect(self.key_pressed)
self.released.connect(self.key_released)
def set_receiver(self, receiver):
self.__receiver = receiver
def key_pressed(self):
self.setStyleSheet("""
border-width: 5px;
border-color: rgb(37,43,52);
color: white;
background-color: rgb(0,187,255);
""",)
def key_released(self):
event = QKeyEvent(QEvent.KeyPress, Qt.Key_A, Qt.NoModifier,
"a", False)
# self.__receiver is my QEditText/QLineEdit/...
self.__receiver.keyPressEvent(event)
This Last part is the one that I post my events on the "self.__receiver". This receiver is set always for the "QWidget" that invoke it.
I have tried just to say something like:
def key_released(self):
event = QKeyEvent(QEvent.KeyPress, Qt.Key_A, Qt.NoModifier,
"a", False)
# web_view is my QWebEngineView... but it won't consume. Or maybe it's not consuming to the right place.
self.web_view.keyPressEvent(event)
This should work when you send the events to the QWebEngineViews focusProxy instead - something like this should work:
recipient = self.web_view.focusProxy()
QApplication.postEvent(recipient, event)

pyqt4 - singleapplication - bring up the original window in an attempt to open the app for the second time

I want only one instance of my app to be running at each time. but when the user attempts to open it the second time, I want the first window to be brought to the front (it could be just minimized or minimized to the corner of the taskbar and the user doesn't know how to open it)
I have this code that does the detection job and it doesn't allow the second instance. I have trouble with the part that it has to open the original window. I have commented out some of my attempt.
import sys
from PyQt4 import QtGui, QtCore
import sys
class SingleApplication(QtGui.QApplication):
def __init__(self, argv, key):
QtGui.QApplication.__init__(self, argv)
self._activationWindow=None
self._memory = QtCore.QSharedMemory(self)
self._memory.setKey(key)
if self._memory.attach():
self._running = True
else:
self._running = False
if not self._memory.create(1):
raise RuntimeError(
self._memory.errorString().toLocal8Bit().data())
def isRunning(self):
return self._running
def activationWindow(self):
return self._activationWindow
def setActivationWindow(self, activationWindow):
self._activationWindow = activationWindow
def activateWindow(self):
if not self._activationWindow:
return
self._activationWindow.setWindowState(
self._activationWindow.windowState() & ~QtCore.Qt.WindowMinimized)
self._activationWindow.raise_()
self._activationWindow.activateWindow()
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.label = QtGui.QLabel(self)
self.label.setText("Hello")
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.label)
if __name__ == '__main__':
key = 'random _ text'
app = SingleApplication(sys.argv, key)
if app.isRunning():
#app.activateWindow()
sys.exit(1)
window = Window()
#app.setActivationWindow(window)
#print app.topLevelWidgets()[0].winId()
window.show()
sys.exit(app.exec_())
I've made this work on Windows using the win32 api (I'm not entirely sure, but there are probably equivalent calls on macos/unix).
Add the following import to your application,
import win32gui
set the window title to a fixed name (instead of doing this, you could store its whndl in the shared memory)
window = Window()
window.setWindowTitle('Single Application Example')
window.show()
and then change your activateWindow method to something like the following:
def activateWindow(self):
# needs to look for a named window ...
whndl = win32gui.FindWindowEx(0, 0, None, "Single Application Example")
if whndl is 0:
return #couldn't find the name window ...
#this requests the window to come to the foreground
win32gui.SetForegroundWindow(whndl)
You might be interested by the solutions proposed here
For instance, I would try:
app = SingleApplication(sys.argv, key)
if app.isRunning():
window = app.activationWindow()
window.showNormal()
window.raise()
app.activateWindow()
sys.exit(1)
window = Window()
app.setActivationWindow(window)
window.setWindowFlags(Popup)
window.show()

make the QMessageBox or QmainWindow in front of any overlapping sibling widgets?

If the active window belongs to some other process,how to make the QMessageBox or QmainWindow of this example in front of any overlapping sibling widgets when timeout ?
I tried raise_() and activateWindow() ,but both don’t work on WinXP
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.resize(800,600)
self.lcdNumber = QLCDNumber()
self.lcdNumber.setNumDigits(8)
layout = QVBoxLayout(self)
layout.addWidget(self.lcdNumber)
self.currentTime = QTime(0,0,0)
self.lcdNumber.display(self.currentTime.toString('hh:mm:ss'))
self.timer = QTimer(self)
self.timer.timeout.connect(self.updateLcdNumberContent)
self.timer.start(1000)
def updateLcdNumberContent(self):
self.currentTime = self.currentTime.addSecs(1)
self.lcdNumber.display(self.currentTime.toString('hh:mm:ss'))
if self.currentTime == QTime(0,0,4) :
msgBox = QMessageBox(self)
msgBox.setWindowTitle('iTimer')
msgBox.setIcon (QMessageBox.Information)
msgBox.setText("Time Out !!")
stopButton = msgBox.addButton("Stop", QMessageBox.ActionRole)
ignoreButton = msgBox.addButton(QMessageBox.Ignore)
stopButton.clicked.connect(self.timer.stop)
msgBox.show()
# self.raise_()
# self.activateWindow()
if __name__ == '__main__':
app =QApplication(sys.argv)
frame = MainWindow()
frame.show()
sys.exit(app.exec_())
Try to modify window flags by using QWidget::setWindowFlags() method of your QMessageBox or QMainWindow. You should use Qt::WindowStaysOnTopHint flag for your purpose.
It will be something like window->setWindowFlags(window->windowFlags() | Qt::WindowStaysOnTopHint).
If you will not succeed with just setWindowFlags(window->windowFlags() | Qt::WindowStaysOnTopHint), you will need to use Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint with another flags. Experiment with it and you'll succeed.

How do I display a PGM file within a QGraphicsView?

I am trying to display a PGM file using the following test application. The code works with a PNG file, but not PGM:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import Image
import ImageQt
class MyView(QGraphicsView):
def __init__(self):
QGraphicsView.__init__(self)
self.scene = QGraphicsScene(self)
self.setScene(self.scene)
img = Image.open('test.pgm') # works with 'test.png'
self.imgQ = ImageQt.ImageQt(img)
pixmap = QPixmap.fromImage(self.imgQ)
self.scene.addPixmap(pixmap)
if __name__ == '__main__':
app = QApplication(sys.argv)
view = MyView()
view.show()
sys.exit(app.exec_())
How can I display a PGM file within the QGraphicsView?
QImage supports reading PGM files. Have you checked that img is loaded correctly?
Perhaps you have abad file, or a typo in the name, or wrong permissions
It looks like you're using PIL to open the image, convert to a QImage and then convert that to a QPixmap.
Is there any reason why you can't just load the PGM directly into a QPixmap and avoid PIL?
Replacing
img = Image.open('test.pgm') # works with 'test.png'
self.imgQ = ImageQt.ImageQt(img)
pixmap = QPixmap.fromImage(self.imgQ)
self.scene.addPixmap(pixmap)
with
pixmap = QPixmap('test.pgm')
self.scene.addPixmap(pixmap)
should work for you.

Resources