everyone.
I want to float a widget inside a layout of a main-window.
the widget is disappered from layout but not displayed on screen
As you can see from following code. I floated two labels 'lbl_title' and 'lbl_icon'
they seems to be floated but not displayed on screen.
Here comes my code.
If you loose commented line, then the lbl_icon and title is removed from layout but not are shown on my screen
from PyQt5.QtCore import QDir, Qt, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QApplication, QFileDialog, QHBoxLayout, QLabel,
QPushButton, QSizePolicy, QSlider, QStyle, QVBoxLayout, QWidget)
from PyQt5.QtWidgets import QMainWindow,QWidget, QPushButton, QAction,QGridLayout
from PyQt5.QtGui import QIcon,QPixmap
import sys
from PyQt5.QtCore import *
class CommonLessonItem(QWidget):
def __init__(self,parent):
super(CommonLessonItem,self).__init__(parent)
self.lbl_title = QLabel(self)
self.lbl_description = QLabel(self)
self.lbl_icon = QLabel(self)
self.__initUI()
self.isChild = False
def __initUI(self):
#set layout
self.layout = QGridLayout(self)
self.layout.addWidget(self.lbl_title,0,0,1,19)
self.layout.addWidget(self.lbl_icon,0,19,1,1)
self.layout.addWidget(self.lbl_description,1,0,1,20)
self.lbl_icon.setWindowFlags(Qt.FramelessWindowHint|Qt.Window)
self.lbl_icon.move(100,100)
self.lbl_title.setWindowFlags(Qt.FramelessWindowHint|Qt.Window)
self.lbl_title.move(100,100)
#initialize info
self.setInfo("Title","Description",None)
self.setLayout(self.layout)
def setInfo(self,title,description,iconPath):
self.lbl_title.setText(title)
self.lbl_description.setText(description)
if(iconPath is not None):
self.lbl_icon.setPixmap(QPixmap(iconPath))
def moveEvent(self,event):
super().moveEvent(event)
if __name__ == "__main__":
app = QApplication(sys.argv)
mw = CommonLessonItem(None)
# mw.setSize(10,200)
mw.show()
sys.exit(app.exec_())
I need your help.
Whenever a widget becomes a top level window by setting the parent to None or, like in your case, setting the Window flag (but I wouldn't suggest that approach) show() must be called.
As explained in windowFlags:
Note: This function calls setParent() when changing the flags for a window, causing the widget to be hidden. You must call show() to make the widget visible again..
Add self.lbl_icon.show() and self.lbl_title.show() after changing their window state.
Related
I'm coming from a tkinter background where everything can be put in a frame.
How can I get my current working codes result (which launches a WebEngine View of a page, google in this instance) to sit inside a main window like shown in the image? Going by the image I want the WebEngine to be housed in the "Green" Box for example.
Working code including all versions used
"""
Python version - 3.7.3
PyQt5 5.15.3
PyQt5-Qt 5.15.2
PyQt5-sip 12.8.1
PyQtWebEngine 5.15.3
PyQtWebEngine-Qt 5.15.2
"""
import sys
from PyQt5.QtWebEngineWidgets import QWebEnginePage
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
url = 'https://google.com'
app = QApplication(sys.argv)
# QWebEngineView
browser = QWebEngineView()
browser.load(QUrl(url))
browser.show()
sys.exit(app.exec_())
You have to use a QGridLayout:
import sys
from PyQt5.QtWidgets import QApplication, QGridLayout, QMainWindow, QTextEdit, QWidget
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
url = "https://google.com"
app = QApplication(sys.argv)
w = QMainWindow()
browser = QWebEngineView()
browser.load(QUrl(url))
central_widget = QWidget()
w.setCentralWidget(central_widget)
lay = QGridLayout(central_widget)
lay.addWidget(browser, 0, 0, 2, 1)
lay.addWidget(QTextEdit(), 0, 1)
lay.addWidget(QTextEdit(), 1, 1)
lay.setColumnStretch(0, 1)
lay.setColumnStretch(1, 1)
lay.setRowStretch(0, 1)
lay.setRowStretch(1, 1)
w.show()
sys.exit(app.exec_())
You could use a vertical layout to hold the two red boxes. And then use a horizontal layout to hold the QWebEngineView and the vertical layout. Zetcode has a good tutorial for layouts. Code heavily borrowed from #eyllanesc:
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow,
QHBoxLayout, QVBoxLayout,
QTextEdit, QWidget)
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
url = "https://google.com"
app = QApplication(sys.argv)
w = QMainWindow()
browser = QWebEngineView()
browser.load(QUrl(url))
central_widget = QWidget()
w.setCentralWidget(central_widget)
vertical = QVBoxLayout()
vertical.addWidget(QTextEdit())
vertical.addWidget(QTextEdit())
horizontal = QHBoxLayout(central_widget)
horizontal.addWidget(browser)
horizontal.addLayout(vertical)
w.show()
sys.exit(app.exec_())
This is entire my code:
import sys
from PySide2.QtCore import Qt
from PySide2.QtWidgets import (
QApplication,
QHBoxLayout,
QLabel,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
v = QVBoxLayout()
h = QHBoxLayout()
for a in range(10):
button = QPushButton(str(a))
button.clicked.connect(lambda checked, a=a: self.button_clicked(a)) # error here
h.addWidget(button)
v.addLayout(h)
self.label = QLabel("")
v.addWidget(self.label)
w = QWidget()
w.setLayout(v)
self.setCentralWidget(w)
def button_clicked(self, n):
self.label.setText(str(n))
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
When I run this code, I get a window like this:
Below the buttons, there is a QLabel, and I want when I click on any button, the button's label will refer to this QLabel, but I get a bunch of confusing errors in the terminal. What's wrong with my code, help me, thanks.
The clicked signal is overload so it accepts 2 signatures where it can send a bool or not. The default signature depends on the library, in this case it seems that PySide2 by default does not send the "checked" parameter, unlike PyQt5 that does.
The solution is to indicate the signature:
button.clicked[bool].connect(lambda checked, a=a: self.button_clicked(a))
Hi, I have an issue with my PyQt5 setWindowIcon.
When I try to set my window icon from a local image, it works perfectly. But when I try to put a online link like:
setWindowIcon( QIcon("https://www.google.ge/images/branding/product/ico/googleg_lodp.ico") )
it does not work. What to do? Its 32x32 ico btw.
~Thanks
You have to use QNetworkAccessManager and manually download image from url. Then read bytes from response, create a QPixmap (beacuse it has loadFromData method) and initialize a QIcon from QPixmap.
And after that you will be able to set window icon.
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
ICON_IMAGE_URL = "https://www.google.ge/images/branding/product/ico/googleg_lodp.ico"
class MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.label = QLabel('Image loading demo')
self.vertical_layout = QVBoxLayout()
self.vertical_layout.addWidget(self.label)
self.setLayout(self.vertical_layout)
self.nam = QNetworkAccessManager()
self.nam.finished.connect(self.set_window_icon_from_response)
self.nam.get(QNetworkRequest(QUrl(ICON_IMAGE_URL)))
def set_window_icon_from_response(self, http_response):
pixmap = QPixmap()
pixmap.loadFromData(http_response.readAll())
icon = QIcon(pixmap)
self.setWindowIcon(icon)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
I want to insert video in blue box(ui image) but I don't know how to insert video file.
My code is here.
I don't know how to add video... Just know example that make video player ...
import sys
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import uic
from PyQt5 import QtCore
from PyQt5.QtCore import QDir, Qt, QUrl, pyqtSlot
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QApplication, QFileDialog, QHBoxLayout, QLabel,
QPushButton, QSizePolicy, QSlider, QStyle, QVBoxLayout, QWidget)
dir_audience=''
dir_movie = ''
dir_export = ''
select_emotion = 'happy'
class Form(QtWidgets.QDialog):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent)
self.ui = uic.loadUi("highlight_export_form.ui", self)
self.ui.show()
self.ui.load_audience.clicked.connect(self.load_audience_clicked)
self.ui.load_movie.clicked.connect(self.load_movie_clicked)
self.ui.start_recog.clicked.connect(self.start_recog_clicked)
self.ui.radio_happy.toggled.connect(self.on_radio_button_toggled)
self.ui.radio_surprised.toggled.connect(self.on_radio_button_toggled)
def load_audience_clicked(self, event):
dir_audience, _ = QFileDialog.getOpenFileName(self, "Open Audience", QDir.homePath())
self.path_audience.setText(dir_audience)
def load_movie_clicked(self, event):
dir_movie, _ = QFileDialog.getOpenFileName(self, "Open Movie", QDir.homePath())
self.path_movie.setText(dir_movie)
def start_recog_clicked(self, event):
self.check_1.setText("start_recognition")
def on_radio_button_toggled(self):
if self.radio_happy.isChecked():
select_emotion='happy'
self.check_3.setText(select_emotion)
elif self.radio_surprised.isChecked():
select_emotion='surprised'
self.check_3.setText(select_emotion)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Form()
sys.exit(app.exec())
Thank you for reading my question.
Qt Designer does not show all the Qt widget, and often we want to add our own widget through Qt, for that there are at least 2 solutions, the first is to create a plugin and load it to Qt Designer, and the other is simpler. promote the widget, the latter is what I will show in this answer.
For this you must make certain minimum changes, I do not know what type of widget is the one you use in the blue box but you must change it to the Widget type that is in the sub-menu of the containers as shown in the following image:
after them you must right click on the widget and select Promote to ..., then a dialogue will appear, in the part of Promoted class name you must place QVideoWidget, and in the part of Header File you must place PyQt5.QtMultimediaWidgets, then press the add button and then Promote:
After that you will be able to use QVideoWidget within your application.
In the following link there is an example
Answer from here was clearer to me:
QWebKit was removed in Qt 5.6. So QWebView is no longer available. Use QWebEngineView as a replacement. In Qt Designer, just add a QWidget to your form and promote it to QWebEngineView (base class: QWidget, header: QWebEngineView). Don't forget to add webenginewidgets to your project file.
Simlar issue: want add QWebEngineView into Qt Designer
for later PySide6 to import and use .ui, exported by Qt Designer
Solution: add QWidget then Promoted to QWebEngineView
Steps
drag a new QWidget into your main ui (window)
right click QWidget -> Promoted to
new popup window, input
Base class Name: QWidget
Promoted class Name: QWebEngineView
Header File: PySide6.QtWebEngineWidgets
== parent class
Global Include: not selected
-> Screenshot
click: Add
click: Promote
I was writing a pyqt5 program for expression evaluator but after running the program i am not able to see any widgets and getting blank window
def expressionevaluator():
import sys
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QWidget,QMainWindow
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Tutorial")
self.setWindowIcon=QtGui.QIcon('pyqt_example2.PNG')
self.home()
def ExitForm(self):
sys.exit()
def home(self):
vbox=QtWidgets.QVBoxLayout()
textbrowser=QtWidgets.QTextBrowser()
lineedit=QtWidgets.QLineEdit()
btn=QtWidgets.QPushButton("QUIT")
btn.clicked.connect(self.close)
vbox.addWidget(textbrowser)
vbox.addWidget(lineedit)
vbox.addWidget(btn)
self.setLayout(vbox)
self.show()
if __name__=="__main__":
app=QApplication(sys.argv)
GUI=Window()
sys.exit(app.exec_())
expressionevaluator()
So what should I do ?
Just running your code I got a widget showing up in my screen, but its components didn't show up. Instead of setting a layout of a QMainWindow try to have a central widget (QWidget) set its layout with its components than set the QMainWindow central widget with this widget. There you go, now you have all working fine.
You had problems with the layout because QMainWindow behaves differently from others Widgets, it has its own layout and many other default behaviors, central widget is the reason why nothing was showing up inside your main window.
def expressionevaluator():
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QTextBrowser
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
class Window(QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Tutorial")
self.setWindowIcon = QIcon('pyqt_example2.PNG')
self.home()
def ExitForm(self):
sys.exit()
def home(self):
vbox = QVBoxLayout()
textbrowser = QTextBrowser()
lineedit = QLineEdit()
btn = QPushButton("QUIT")
central_widget = QWidget()
central_widget.setLayout(vbox)
btn.clicked.connect(self.close)
vbox.addWidget(textbrowser)
vbox.addWidget(lineedit)
vbox.addWidget(btn)
self.setCentralWidget(central_widget)
self.show()
if __name__=="__main__":
app = QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())
expressionevaluator()
Note: There are many improvements in the structure of your code you could do, I just changed as less as I could to make it work, for example try to not import all the modules at once, import just what you need for example QIcon, QLineEdit and so on, instead of the whole QtWidgets, or QtCore...
Following code works well:
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QTextBrowser
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
class Window(QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Tutorial")
self.setWindowIcon = QIcon('pyqt_example2.PNG')
self.home()
def ExitForm(self):
sys.exit()
def home(self):
vbox = QVBoxLayout()
textbrowser = QTextBrowser()
lineedit = QLineEdit()
btn = QPushButton("QUIT")
central_widget = QWidget()
central_widget.setLayout(vbox)
btn.clicked.connect(self.ExitForm)
vbox.addWidget(textbrowser)
vbox.addWidget(lineedit)
vbox.addWidget(btn)
self.setCentralWidget(central_widget)
self.show()
if __name__=="__main__":
app = QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())