I am developing a PyQt5 application however I am having issues with the heights of the widgets. Below is a simplified version of my issue:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class App(QWidget):
def __init__(self):
super().__init__()
self.showMaximized()
self.setStyleSheet("QWidget {background: blue;}")
print(self.frameGeometry().height())
self.show()
if __name__ == "__main__":
window = QApplication(sys.argv)
app = App()
window.setStyle(QStyleFactory.create("Fusion"))
window.exec_()
Here I create a window and maximise it. Using a tkinter window, it tells me the height maximised is 841, which is the size of my screen, however the PyQt5 application prints the height to be 519. Is this an issue with the self.showMaximized() method, or some other issue.
Resizing is not instantaneous in Qt. What Qt does is take the information from showMaximized to activate the flag of the native window (library that depends on each OS) then after a time T the OS applies that flag and sends it the new geometry. So in your case you have to give it a delay to get the correct information.
import sys
from PyQt5.QtWidgets import QApplication, QStyleFactory, QWidget
from PyQt5.QtCore import QTimer
class App(QWidget):
def __init__(self):
super().__init__()
self.setStyleSheet("QWidget {background: blue;}")
self.showMaximized()
QTimer.singleShot(100, self.calculate)
def calculate(self):
print(self.frameGeometry().height())
if __name__ == "__main__":
window = QApplication(sys.argv)
app = App()
window.setStyle(QStyleFactory.create("Fusion"))
window.exec_()
On the other hand, if your objective is to know the size of the initial screen then you should not use a QWidget for that since it will depend on the time it takes for Qt and the native library to create the native window, instead use the Screen class :
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QGuiApplication
if __name__ == "__main__":
window = QApplication(sys.argv)
print(QGuiApplication.primaryScreen().availableGeometry().height())
Related
I was just creating my window in the Qt Designer, when I had finished it I noticed that even opening it from a .py file, the maximize and restore buttons in the title bar are disabled, they appear in there but I can't click it. I am using Windows 10 by the way. It appears like this:
enter image description here
I found some solutions using:
self.setWindowFlag(Qt.WindowMaximizeButtonHint) self.setWindowFlag(Qt.WindowMinimizeButtonHint)
but they don't work in my case.
Here's the complete code:
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtMultimedia import *
import sys
class mainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(mainWindow, self).__init__()
uic.loadUi('mainMenu.ui', self)
self.show()
self.setWindowFlag(Qt.WindowMaximizeButtonHint)
self.setWindowFlag(Qt.WindowMinimizeButtonHint)
app = QtWidgets.QApplication(sys.argv)
window = mainWindow()
app.exec_()
It's a tiny simple code.
In this code, self.showMaximized() is not working.
And even it's so tiny, I don't know why.
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Window(QWindow):
def __init__(self):
QWindow.__init__(self)
self.setTitle("title")
self.showMaximized()
# self.resize(400,300)
# self.showMaximized()
# self.showFullScreen()
app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())
Delete 'screen.show()', and then showMaximized() worked.
Either you need to use .showMaximized() only on newly created Object i.e., screen, but not in your constructor or only at the end of your constructor, but not twice.
Code:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Window(QWindow):
def __init__(self):
QWindow.__init__(self)
self.setTitle("title")
app = QApplication(sys.argv)
screen = Window()
screen.showMaximized()
sys.exit(app.exec_())
I want to create a splash screen in PyQt5 using Python. I searched but I found in Pyqt4 and I have no understanding of PyQt4 so help me in this case I would be gratful
Splash screen in pyqt
I like to add it in just before i load my main widget with a slight fade - note this is only useful to show a logo, if your application has a long load time you can utilise the splash screen like #S. Nick has shown to allow load time whilst you show the splashscreen:
from PyQt5 import QtGui, QtCore, QtWidgets
import time
if __name__ == '__main__':
app = QtWidgets.QApplication([])
# Create splashscreen
splash_pix = QtGui.QPixmap('picture.png')
splash = QtWidgets.QSplashScreen(splash_pix, QtCore.Qt.WindowStaysOnTopHint)
# add fade to splashscreen
opaqueness = 0.0
step = 0.1
splash.setWindowOpacity(opaqueness)
splash.show()
while opaqueness < 1:
splash.setWindowOpacity(opaqueness)
time.sleep(step) # Gradually appears
opaqueness+=step
time.sleep(1) # hold image on screen for a while
splash.close() # close the splash screen
#widget = YourWidget()
#widget.show() # This is where you'd run the normal application
app.exec_()
Try it:
import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QDialog, QPushButton, QVBoxLayout, QApplication, QSplashScreen
from PyQt5.QtCore import QTimer
class Dialog(QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.b1 = QPushButton('Display screensaver')
self.b1.clicked.connect(self.flashSplash)
layout = QVBoxLayout()
self.setLayout(layout)
layout.addWidget(self.b1)
def flashSplash(self):
self.splash = QSplashScreen(QPixmap('D:/_Qt/img/pyqt.jpg'))
# By default, SplashScreen will be in the center of the screen.
# You can move it to a specific location if you want:
# self.splash.move(10,10)
self.splash.show()
# Close SplashScreen after 2 seconds (2000 ms)
QTimer.singleShot(2000, self.splash.close)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Dialog()
main.show()
sys.exit(app.exec_())
Example 2
import sys
from PyQt5 import QtCore, QtGui, QtWidgets # + QtWidgets
import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtCore import QTimer, Qt
if __name__ == '__main__':
app = QApplication(sys.argv)
label = QLabel("""
<font color=red size=128>
<b>Hello PyQt, The window will disappear after 5 seconds!</b>
</font>""")
# SplashScreen - Indicates that the window is a splash screen. This is the default type for .QSplashScreen
# FramelessWindowHint - Creates a borderless window. The user cannot move or resize the borderless window through the window system.
label.setWindowFlags(Qt.SplashScreen | Qt.FramelessWindowHint)
label.show()
# Automatically exit after 5 seconds
QTimer.singleShot(5000, app.quit)
sys.exit(app.exec_())
The simple and best example that I found.
https://www.youtube.com/watch?v=TsatZJfzb_Q&t=162s
from PyQt5.QtWidgets import QMainWindow, QApplication, QDialog
from PyQt5.QtWidgets import QGraphicsScene,QSplashScreen
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
class SplashScreen(QSplashScreen):
def __init__(self):
super(QSplashScreen, self).__init__()
loadUi("splash.ui", self)
self.setWindowFlag(Qt.FramelessWindowHint)
pixmap = QPixmap("any_image.jpg")
self.setPixmap(pixmap)
def progress(self):
for i in range(40):
time.sleep(0.1)
self.progressBar.setValue(i)
class MainScreen(QMainWindow):
def function1():
.......
def function2():
......
if __name__ == "__main__":
app = QApplication(sys.argv)
splash = SplashScreen()
splash.show()
splash.progress()
mainscreen = MainScreen()
mainscreen.show()
splash.finish(widget)
sys.exit(app.exec_())
So I've been trying to create my own terminal but that has been proven very glitchy and not professional looking.
Then I stumbled across this code which is for PyQt4:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class embterminal(QWidget):
def __init__(self):
QWidget.__init__(self)
self.process = QProcess(self)
self.terminal = QWidget(self)
layout = QVBoxLayout(self)
layout.addWidget(self.terminal)
#self.process.start(
#'xterm',['-into', str(self.terminal.winId())])
# Works also with urxvt:
self.process.start(
'urxvt',['-embed', str(self.terminal.winId())])
if __name__ == "__main__":
app = QApplication(sys.argv)
main = embterminal()
main.show()
sys.exit(app.exec_())
Since my application is written in PyQt5, I naturally tried porting that code to PyQt5.
I changed from PyQt4.QtCore import *
from PyQt4.QtGui import * to from PyQt5.QtCore import *
from PyQt5.QtGui import * and added from PyQt5.QtWidgets import *
Then when I ran my code I realized the terminal didn't pop up.
I wonder why does this happen and is there a workaround ?
I also wonder if I can use both PyQt5 and PyQt4 in the same project/file, even.
In PyQt. QWidget.winId() returns a sip.voidptr object, but if you convert it to an integer, it should work. Here's a working example:
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.process = QProcess(self)
self.terminal = QWidget(self)
layout = QVBoxLayout(self)
layout.addWidget(self.terminal)
wid = str(int(self.terminal.winId()))
self.process.start('urxvt', ['-embed', wid])
def closeEvent(self, event):
self.process.terminate()
self.process.waitForFinished(1000)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.setGeometry(100, 100, 800, 600)
window.show()
sys.exit(app.exec_())
I have a form with a picture on it, in this example it is a Humidity Indicator the user should be able to press the 60, 10 and 5% spot. The label should display the pressed spot.
How do I do that could somebody show me an example or is this not possible with Qt?
My code (which is pretty empty for now)
from PyQt5.QtWidgets import QApplication, QWidget, QDialog
from PyQt5.QtGui import QPixmap, QRegExpValidator
from PyQt5.QtCore import QRegExp
from mysql.connector import (connection)
from datetime import *
from bs4 import BeautifulSoup as bs
import os
import sys
import DatabaseHandling
'''Convert UI file to Python'''
os.chdir("C:\\Users\Gianni Declercq\AppData\Local\Programs\Python\Python36-32\Scripts")
os.system("pyuic5.exe M:\QtProjects\\Ui\RPI1_Third.ui -o M:\QtProjects\\RPI1_Third_ui.py")
from RPI1_Third_ui import Ui_Form3 # import after recreation of py file
class ThirdWindow(QWidget, Ui_Form3):
def __init__(self):
super(ThirdWindow, self).__init__()
self.dbu = DatabaseHandling.DatabaseUtility()
self.msl = None
# Show UI on screen + resize window
self.setupUi(self)
self.picInidicator.setPixmap(QPixmap("F:\QtProjects\\138691.jpg"))
self.setFixedSize(800, 480)
if __name__ == '__main__':
app = QApplication([])
window = ThirdWindow()
window.show()
sys.exit(app.exec_())
Example of the form