I have defined a simple main window and second pop-up window. When I call the MainWindow.create_new_window() method, the SecondWindow does not show up as a new window but its QLabel is created within the MainWindow instance. Here's the code:
import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QLabel, QWidget, QVBoxLayout
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.second_window = None
self.main_layout = QVBoxLayout(self)
self.new_window_button = QPushButton('New Window', self)
self.new_window_button.clicked.connect(self.create_new_window)
self.main_layout.addWidget(self.new_window_button)
def create_new_window(self):
if self.second_window is None:
self.second_window = SecondWindow(self)
self.second_window.show()
class SecondWindow(QWidget):
def __init__(self, *args, **kwargs):
super(SecondWindow, self).__init__(*args, **kwargs)
self.main_layout = QVBoxLayout(self)
self.hello_label = QLabel('Hello I am the second window.', self)
self.main_layout.addWidget(self.hello_label)
if __name__ == '__main__':
app = QApplication(sys.argv)
mainwin = MainWindow()
mainwin.show()
sys.exit(app.exec_())
When I create the second window without specifying the MainWindow instance as the parent (self.second_window = SecondWindow()), it opens as expected. Can anyone tell me what's going on here?
By default, a QWidget that has a parent implies that the widget will be placed inside the parent, so you observe that behavior.
If you want it to be a window then you must activate the flag Qt::Window
# ...
from PyQt5.QtCore import Qt
# ...
class SecondWindow(QWidget):
def __init__(self, *args, **kwargs):
super(SecondWindow, self).__init__(*args, **kwargs)
self.setWindowFlags(self.windowFlags() | Qt.Window) # <---
# ...
Other options is to use a QDialog that is a type of widget that by default already has that flag activated and whose objective is to ask the user for information.
From documentation:
If parent is 0, the new widget becomes a window. If parent is another widget, this widget becomes a child window inside parent. The new widget is deleted when its parent is deleted.
When I run your code, I get that new widget inside of the main one - as is described in documentation.
So basically, you should set parent to a QWidget only if you indend to use it as a window's widget (insert it in a layout, or use it as central widget etc.); if you want to use it as-is you don't need a parent.
If you need your widget to have a parent and be a separate window, better idea may be to use QDialog instead of QWidget. Just make your SecondWindow class subclass QDialog instead and you're good to go.
Example code (I've changed both your Windows to QDialog):
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QLabel, QApplication, QDialog
class MainWindow(QDialog):
def __init__(self):
super(MainWindow, self).__init__()
self.second_window = None
self.main_layout = QVBoxLayout(self)
self.new_window_button = QPushButton('New Window', self)
self.new_window_button.clicked.connect(self.create_new_window)
self.main_layout.addWidget(self.new_window_button)
def create_new_window(self):
if self.second_window is None:
self.second_window = SecondWindow(self)
# set second window as modal, because MainWindow is QDialog/QWidget.
self.setModal(True)
self.second_window.show()
class SecondWindow(QDialog):
def __init__(self, *args, **kwargs):
super(SecondWindow, self).__init__(*args, **kwargs)
self.main_layout = QVBoxLayout(self)
self.hello_label = QLabel('Hello I am the second window.', self)
self.main_layout.addWidget(self.hello_label)
if __name__ == '__main__':
app = QApplication(sys.argv)
mainwin = MainWindow()
mainwin.show()
sys.exit(app.exec_())
In your imported packages import that staff:
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel
Related
I have a QWidget containing another (child) widget for which I'd like to process hoverEnterEvent and hoverLeaveEvent. The documentation mentions that
Mouse events occur when a mouse cursor is moved into, out of, or within a widget, and if the widget has the Qt::WA_Hover attribute.
So I tried to receive the hover events by setting this attribute and implementing the corresponding event handlers:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
class TestWidget(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
layout.addWidget(TestLabel('Test 1'))
layout.addWidget(TestLabel('Test 2'))
self.setLayout(layout)
self.setAttribute(Qt.WA_Hover)
class TestLabel(QLabel):
def __init__(self, text):
super().__init__(text)
self.setAttribute(Qt.WA_Hover)
def hoverEnterEvent(self, event): # this is never invoked
print(f'{self.text()} hover enter')
def hoverLeaveEvent(self, event): # this is never invoked
print(f'{self.text()} hover leave')
def mousePressEvent(self, event):
print(f'{self.text()} mouse press')
app = QApplication([])
window = TestWidget()
window.show()
sys.exit(app.exec_())
However it doesn't seem to work, no hover events are received. The mousePressEvent on the other hand does work.
In addition I tried also the following things:
Set self.setMouseTracking(True) for all widgets,
Wrap the TestWidget in a QMainWindow (though that's not what I want to do for the real application),
Implement event handlers on parent widgets and event.accept() (though as I understand it, events propagate from inside out, so this shouldn't be required).
How can I receive hover events on my custom QWidgets?
The QWidget like the QLabel do not have the hoverEnterEvent and hoverLeaveEvent methods, those methods are from the QGraphicsItem so your code doesn't work.
If you want to listen to the hover events of the type you must override the event() method:
import sys
from PyQt5.QtCore import Qt, QEvent
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
class TestWidget(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout(self)
layout.addWidget(TestLabel("Test 1"))
layout.addWidget(TestLabel("Test 2"))
class TestLabel(QLabel):
def __init__(self, text):
super().__init__(text)
self.setAttribute(Qt.WA_Hover)
def event(self, event):
if event.type() == QEvent.HoverEnter:
print("enter")
elif event.type() == QEvent.HoverLeave:
print("leave")
return super().event(event)
def main():
app = QApplication(sys.argv)
window = TestWidget()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Did you know that you can do this with QWidget's enterEvent and leaveEvent? All you need to do is change the method names. You won't even need to set the Hover attribute on the label.
from PyQt5.QtWidgets import QApplication, QGridLayout, QLabel, QWidget
class Window(QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
layout = QGridLayout()
self.label = MyLabel(self)
layout.addWidget(self.label)
self.setLayout(layout)
text = "hover label"
self.label.setText(text)
class MyLabel(QLabel):
def __init__(self, parent=None):
super(MyLabel, self).__init__(parent)
self.setParent(parent)
def enterEvent(self, event):
self.prev_text = self.text()
self.setText('hovering')
def leaveEvent(self, event):
self.setText(self.prev_text)
if __name__ == "__main__":
app = QApplication([])
w = Window()
w.show()
app.exit(app.exec_())
QButtonGroups can have checkboxes. But you cannot add them to a QButtonGroup because they do not inherit QAbstractButton.
It would be really nice for some UIs to be able to have a few QGroupBoxes with exclusive checkboxes. That is, you check one and the other QGroupBoxes are automatically unchecked.
In an ideal world, I could do something like this:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QGroupBox, QWidget, QApplication,
QAbstractButton, QButtonGroup)
class SuperGroup(QGroupBox, QAbstractButton):
def __init__(self, title, parent=None):
super(SuperGroup, self).__init__(title, parent)
self.setCheckable(True)
self.setChecked(False)
class Example(QWidget):
def __init__(self):
super().__init__()
sg1 = SuperGroup(title = 'Super Group 1', parent = self)
sg1.resize(200,200)
sg1.move(20,20)
sg2 = SuperGroup(title = 'Super Group 2', parent = self)
sg2.resize(200,200)
sg2.move(300,20)
self.bgrp = QButtonGroup()
self.bgrp.addButton(sg1)
self.bgrp.addButton(sg2)
self.setGeometry(300, 300, 650, 500)
self.setWindowTitle('SuperGroups!')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
This code fails as soon as you try to add a SuperGroup to the button group. PyQt5 explicitly does not support multiple inheritance. But there are some examples out in the wild, like from this blog.
In this simple example, it would be easy to manage the clicks programmatically. But as you add more group boxes, it gets more messy. Or what if you want a QButtonGroup with buttons, check boxes, and group boxes? Ugh.
It is not necessary to create a class that inherits from QGroupBox and QAbstractButton (plus it is not possible in pyqt or Qt/C++). The solution is to create a QObject that handles the states of the other QGroupBox when any QGroupBox is checked, and I implemented that for an old answer for Qt/C++ so this answer is just a translation:
import sys
from PyQt5.QtCore import pyqtSlot, QObject, Qt
from PyQt5.QtWidgets import QGroupBox, QWidget, QApplication, QButtonGroup
class GroupBoxManager(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self._groups = []
#property
def groups(self):
return self._groups
def add_group(self, group):
if isinstance(group, QGroupBox):
group.toggled.connect(self.on_toggled)
self.groups.append(group)
#pyqtSlot(bool)
def on_toggled(self, state):
group = self.sender()
if state:
for g in self.groups:
if g != group and g.isChecked():
g.blockSignals(True)
g.setChecked(False)
g.blockSignals(False)
else:
group.blockSignals(True)
group.setChecked(False)
group.blockSignals(False)
class Example(QWidget):
def __init__(self):
super().__init__()
sg1 = QGroupBox(
title="Super Group 1", parent=self, checkable=True, checked=False
)
sg1.resize(200, 200)
sg1.move(20, 20)
sg2 = QGroupBox(
title="Super Group 2", parent=self, checkable=True, checked=False
)
sg2.resize(200, 200)
sg2.move(300, 20)
self.bgrp = GroupBoxManager()
self.bgrp.add_group(sg1)
self.bgrp.add_group(sg2)
self.setGeometry(300, 300, 650, 500)
self.setWindowTitle("SuperGroups!")
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
I'm trying to write a Python program using PyQt5 that will display a window in each iteration of the for loop. I would like to close after incrementing and displaying the next window. However, I do not know how to stop the loop every iteration and at the moment I am getting 6 windows at once.
main.py
import sys
from PyQt5.QtWidgets import (QLineEdit, QVBoxLayout, QMainWindow,
QWidget, QDesktopWidget, QApplication, QPushButton, QLabel,
QComboBox, QFileDialog, QRadioButton)
from PyQt5.QtCore import pyqtSlot, QByteArray
from alert import Window2
from test import test
class SG(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.resize(300, 150)
self.setWindowTitle('TEST')
self.resultsGen = QPushButton('TEST', self)
self.resultsGen.clicked.connect(lambda: self.on_click())
self.show()
#pyqtSlot()
def on_click(self):
test(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
sg = SG()
sys.exit(app.exec_())
alert.py
from PyQt5.QtWidgets import (QLineEdit, QVBoxLayout, QMainWindow,
QWidget, QDesktopWidget, QApplication, QPushButton, QLabel,
QComboBox, QFileDialog, QRadioButton)
from PyQt5.QtCore import pyqtSlot, QByteArray
from PyQt5.QtGui import QPixmap
from PyQt5 import QtGui, QtCore
class Window2(QMainWindow):
def __init__(self):
super().__init__()
self.initPopup()
def initPopup(self):
self.resize(500, 500)
self.setWindowTitle("Window22222")
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
lay = QVBoxLayout(self.central_widget)
label = QLabel(self)
pixmap = QPixmap('cropped/8.png')
label.setPixmap(pixmap)
self.resize(pixmap.width(), pixmap.height())
lay.addWidget(label)
self.textbox = QLineEdit(self)
self.textbox.move(20, 20)
self.textbox.resize(280, 40)
# Create a button in the window
self.button = QPushButton('Show text', self)
self.button.move(20, 80)
# connect button to function on_click
self.button.clicked.connect(lambda: self.on_clickX())
self.show()
#pyqtSlot()
def on_clickX(self):
textboxValue = self.textbox.text()
print(textboxValue)
self.textbox.setText("")
self.hide()
test.py
from alert import Window2
def test(self):
for x in range(6):
w = Window2()
As soon as you run the for cycle, all the code of the initialization will be executed, which includes the show() call you used at the end of initPopup().
A simple solution is to create a new signal that is emitted whenever you hide a window, and connect that signal to a function that creates a new one until it reaches the maximum number.
main.py:
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton
from alert import Window2
class SG(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.alerts = []
def initUI(self):
self.resize(300, 150)
self.setWindowTitle('TEST')
self.resultsGen = QPushButton('TEST', self)
self.resultsGen.clicked.connect(self.nextAlert)
self.show()
def nextAlert(self):
if len(self.alerts) >= 6:
return
alert = Window2()
self.alerts.append(alert)
alert.setWindowTitle('Window {}'.format(len(self.alerts)))
alert.closed.connect(self.nextAlert)
alert.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
sg = SG()
sys.exit(app.exec_())
alert.py:
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Window2(QMainWindow):
closed = pyqtSignal()
def __init__(self):
super().__init__()
self.initPopup()
def initPopup(self):
self.resize(500, 500)
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
lay = QVBoxLayout(self.central_widget)
label = QLabel(self)
pixmap = QPixmap('cropped/8.png')
label.setPixmap(pixmap)
self.resize(pixmap.width(), pixmap.height())
lay.addWidget(label)
self.textbox = QLineEdit(self)
lay.addWidget(self.textbox)
# Create a button in the window
self.button = QPushButton('Show text', self)
lay.addWidget(self.button)
# connect button to function on_click
self.button.clicked.connect(lambda: self.on_clickX())
self.show()
#pyqtSlot()
def on_clickX(self):
textboxValue = self.textbox.text()
print(textboxValue)
self.textbox.setText("")
self.hide()
self.closed.emit()
Just note that with this very simplified example the user might click on the button of the "SG" widget even if an "alert" window is visibile. You might prefer to use a QDialog instead of a QMainWindow and make the main widget a parent of that dialog.
main.py:
class SG(QWidget):
# ...
def nextAlert(self):
if len(self.alerts) >= 6:
return
alert = Window2(self)
# ...
alert.py:
class Window2(QDialog):
closed = pyqtSignal()
def __init__(self, parent):
super().__init__()
self.initPopup()
def initPopup(self):
self.resize(500, 500)
# a QDialog doesn't need a central widget
lay = QVBoxLayout(self)
# ...
Also, if an alert window is closed using the "X" button the new one will not be shown automatically. To avoid that, you can implement the "closeEvent" and ignore the event, so that the user will not be able to close the window until the button is clicked. As QDialogs can close themself when pressing the escape key, I'm also ignoring that situation.
alert.py:
class Window2(QMainWindow):
# ...
def closeEvent(self, event):
event.ignore()
def keyPressEvent(self, event):
if event.key() != Qt.Key_Escape:
super().keyPressEvent(event)
I want a single menubar in my main window and be able to set the menus in the menubar from additional classes. Using the setMenuWidget command will overwrite the first menu option as shown in the code. In the classes where I set up the menu I think I may need to just set up a menu rather than a menubar, then set up the menubar in the main window.
This is what I would l like, which can be achieved by populating a single menubar in a class, though I am trying to avoid this method.
Instead only the second menu is show
import sys
from PyQt5.QtWidgets import QAction, QApplication, QMainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
class ToolBar0(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self)
bar = self.menuBar() # don't think I need a menubar here
file_menu = bar.addMenu('menu1')
one = QAction('one', self)
two = QAction('two', self)
file_menu.addAction(one)
file_menu.addAction(two)
class ToolBar1(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self)
bar = self.menuBar() # don't think I need a menubar here
file_menu = bar.addMenu('menu2')
one = QAction('one', self)
two = QAction('two', self)
file_menu.addAction(one)
file_menu.addAction(two)
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self, parent=None)
#should a menubar be set up here?
#For seting widgets in main window
self.Tool_Bar0 = ToolBar0(self)
self.setMenuWidget(self.Tool_Bar0)
###menu_bar0 is over written
self.Tool_Bar1 = ToolBar1(self)
#self.setMenuWidget(self.Tool_Bar1)
if __name__ == '__main__':
app = QApplication(sys.argv)
# creating main window
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
You could use a base class with a method to return either a list of QMenu items containing QAction items or a list of QAction items and then render them in your QMainWindow toolbar in whichever way you want, here is an example:
import sys
from PyQt5.QtWidgets import QAction, QApplication, QMainWindow, QMenu
class WindowWithToolbar:
def __init__(self):
super().__init__()
def menu_items(self)->list:
pass
class Window1(WindowWithToolbar, QMainWindow):
def __init__(self):
WindowWithToolbar.__init__(self)
QMainWindow.__init__(self)
# New menu with actions
self.menu = QMenu('one')
self.menu.addActions([QAction('two', self), QAction('three', self)])
def menu_items(self):
return [self.menu]
class Window2(WindowWithToolbar, QMainWindow):
def __init__(self):
WindowWithToolbar.__init__(self)
QMainWindow.__init__(self)
def menu_items(self):
# Only actions
return [QAction('three', self), QAction('four', self)]
class MainWindow(WindowWithToolbar, QMainWindow):
def __init__(self):
QMainWindow.__init__(self, parent=None)
self.window1 = Window1()
self.window2 = Window2()
self.menu = QMenu('File')
self.helloAction = QAction('Hello')
self.menu.addAction(self.helloAction)
self._build_menu()
def menu_items(self)->list:
return [self.menu]
def _build_menu(self):
self._add_menu_items(self)
self._add_menu_items(self.window1)
self._add_menu_items(self.window2)
def _add_menu_items(self, windowWithToolbar: WindowWithToolbar):
for menu_item in windowWithToolbar.menu_items():
if isinstance(menu_item, QMenu):
self.menuBar().addMenu(menu_item)
elif isinstance(menu_item, QAction):
self.menuBar().addAction(menu_item)
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
so I'm pretty new to Python. And for the life of me can't figure out why the following code isn't working (I'm using PyQt5). I'm basically trying to have 2 widgets inside a stackedwidget so I can switch between them. And the button to switch from window 0 to window 1 would be in window 0 obviously. So I would need to be able to somehow reference the stackedwidget. But when I try to pass the stackedwidget as a reference, it complains that the variable is None, even when that shouldn't be the case.
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget, QGridLayout, QPushButton, QHBoxLayout, QVBoxLayout, QStackedWidget
from PyQt5.QtCore import QCoreApplication
class DeviceSelectionWindow(QWidget):
def __init__(self, mainWindow):
super().__init__()
self.initUI()
def initUI(self):
testB = QPushButton("test",self)
#------------------------------------------------------------
class ModeSelectionWindow(QWidget):
def __init__(self, mainWindow):
super().__init__()
self.mainWindow = mainWindow
self.initUI()
def initUI(self):
recordButton = QPushButton("Record tutorial")
watchButton = QPushButton("Watch tutorial")
recordButton.setFixedSize(200,100)
recordButton.setStyleSheet("font-size:30px")
watchButton.setFixedSize(200,100)
watchButton.setStyleSheet("font-size:30px")
recordButton.clicked.connect(self.mainWindow.setCurrentIndex(1))
#Add horizontal strech layout box (centered)
hbox = QHBoxLayout()
hbox.addWidget(recordButton)
hbox.addWidget(watchButton)
#Add vertical strech layout box (centered)
vbox = QVBoxLayout()
vbox.addLayout(hbox)
self.setLayout(vbox)
#------------------------------------------------------------
class MainWindow(QStackedWidget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.initUI()
def initUI(self):
self.resize(1200,600)
self.centerWindow()
self.setWindowTitle("MultiPov Tutorial")
modeSelectionWindow = ModeSelectionWindow(self)
deviceSelectionWindow = DeviceSelectionWindow(self)
self.addWidget(modeSelectionWindow)
self.addWidget(deviceSelectionWindow)
self.show()
def centerWindow(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
#------------------------------------------------------------
if __name__=='__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
sys.exit(app.exec_())
The connect method takes a function name, you're doing a function call inside it which return None.
recordButton.clicked.connect(self.mainWindow.setCurrentIndex(1))
One way to get around this is to write a method that does the work (change the index)
def setWindow1(self):
self.mainWindow.setCurrentIndex(1)
Then connect that method to the clicked signal
recordButton.clicked.connect(self.setWindow1)
Or, use a lambda function and get it done in one line
recordButton.clicked.connect(lambda: self.mainWindow.setCurrentIndex(1))