PyQt5 - QFrame size is ignored in window - python

I was creating an application with a QFrame on the left side and a control panel on the right. However, I cannot get the QFrame on the left to be sized properly. I created the following example to demonstrate the problem:
import sys
from PyQt5.QtWidgets import QFrame, QApplication, QWidget, QVBoxLayout, QHBoxLayout, \
QLabel
class MainWindow(QWidget):
"""Main Windows for this demo."""
def __init__(self):
"""Constructor."""
super().__init__()
self.frame = MyFrame(self)
layout_main = QHBoxLayout(self)
layout_left = QVBoxLayout()
layout_right = QVBoxLayout()
layout_main.addLayout(layout_left)
layout_main.addLayout(layout_right)
self.frame.resize(600, 600)
layout_left.addWidget(self.frame)
self.label = QLabel('I am on the right')
layout_right.addWidget(self.label)
# self.setGeometry(300, 100, 900, 900)
self.show()
class MyFrame(QFrame):
"""Custom frame."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setFrameStyle(QFrame.Panel | QFrame.Raised)
self.setStyleSheet('QFrame { background-color: red; }')
def main():
"""Main function."""
app = QApplication([])
window = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I would expect a big red shape on the left but instead I get this:
Resizing the window (either at runtime by dragging or by setting the geometry in code) does resize the QFrame to neatly fill up half the screen. But I want it to have a predefined fixed size.
Why is frame.resize not working as expected?

Found it. Using frame.setFixedSize() does exactly what I want:
class MyFrame(QFrame):
def __init__(self, *args, **kwargs):
self.setFixedSize(300, 300) # < Added this line
The frame keeps it size and if I resize the whole window this is respected:
I am still not sure why resize() does nothing.

Related

How to edit only background-color for PyQT5 widgets

I have a problem with styles in PyQT5.
I would like to modify something in the "Fusion" style : when the page loses focus, the blue of some widgets becomes white, i would like to keep them blue.
But when i try to edit only the background color for a QprogressBar, the text is no more centered and there are some other changes.
(app.setStyleSheet("QProgressBar::chunk { background-color : blue}"))
I also tried app.my_progress_bar.setStyleSheed("background-color : blue") which seems to keep text centered but i don't know how to do it for "chunk" item.
Here is a little script if you want to test a solution :
import sys
import time
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QWidget, QPushButton, QProgressBar, QVBoxLayout, QApplication
class Thread(QThread):
_signal = pyqtSignal(int)
def __init__(self):
super(Thread, self).__init__()
def __del__(self):
self.wait()
def run(self):
for i in range(100):
time.sleep(0.1)
self._signal.emit(i)
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.setWindowTitle('QProgressBar')
self.btn = QPushButton('Click me')
self.btn.clicked.connect(self.btnFunc)
self.pbar = QProgressBar(self)
self.pbar.setValue(0)
self.resize(300, 100)
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.pbar)
self.vbox.addWidget(self.btn)
self.setLayout(self.vbox)
self.show()
def btnFunc(self):
self.thread = Thread()
self.thread._signal.connect(self.signal_accept)
self.thread.start()
self.btn.setEnabled(False)
def signal_accept(self, msg):
self.pbar.setValue(int(msg))
if self.pbar.value() == 99:
self.pbar.setValue(0)
self.btn.setEnabled(True)
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setStyle("Fusion") ##### When the main windows loses focus, the progressbar becomes white instead of blue
ex = Example()
ex.show()
sys.exit(app.exec_())
When the window have the focus :
When the window does not have the focus :
There is no need to use style sheets as long as the color roles of the widget are known.
Specifically, QProgressBar normally uses the Highlight role, which has a different color for the Inactive color group, so you just need to override it.
palette = self.pbar.palette()
palette.setBrush(
palette.Inactive, palette.Highlight, palette.highlight())
self.pbar.setPalette(palette)
Note that the palette is only a reference, it's completely up to the style to decide which group/role use for a widget (or even completely ignore it). If you use another style than Fusion, the above might not work as expected.
use stylesheet for QProgressBar to achieve centered text and QProgressBar::chunk to keep background color even if it loses focus
import sys
import time
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QWidget, QPushButton, QProgressBar, QVBoxLayout, QApplication
class Thread(QThread):
_signal = pyqtSignal(int)
def __init__(self):
super(Thread, self).__init__()
def __del__(self):
self.wait()
def run(self):
for i in range(100):
time.sleep(0.1)
self._signal.emit(i)
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.setWindowTitle('QProgressBar')
self.btn = QPushButton('Click me')
self.btn.clicked.connect(self.btnFunc)
self.pbar = QProgressBar(self)
self.pbar.setValue(0)
# setting background color
self.pbar.setStyleSheet(
"QProgressBar"
"{"
"text-align:center;"
"}"
"QProgressBar::chunk"
"{"
"background-color : blue;"
"text-align:center;"
"color : #E0E0E0;"
"border : 1px"
"}"
)
self.resize(300, 100)
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.pbar)
self.vbox.addWidget(self.btn)
self.setLayout(self.vbox)
self.show()
def btnFunc(self):
self.thread = Thread()
self.thread._signal.connect(self.signal_accept)
self.thread.start()
self.btn.setEnabled(False)
def signal_accept(self, msg):
self.pbar.setValue(int(msg))
if self.pbar.value() == 99:
self.pbar.setValue(0)
self.btn.setEnabled(True)
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setStyle("Fusion") ##### When the main windows loses focus, the progressbar becomes white instead of blue
ex = Example()
ex.show()
sys.exit(app.exec_())

How to make QLabel subwidgets inside a QScrollArea use wordwrap instead of expanding the container widget?

My code has two files. One is the "main" file that uses a fixed size QFrame instead of the standard window frame. The second is also a subclassed QWidget for adding to the QScrollArea in the main widget.
I want the QFrame of the subwidget to be fixed and the label to actually use the wordwrap property instead of just making it longer to fit and screwing up the whole QScrollArea scrolling area. I have tried using QSizePolicy for the container_widget and QFrame of the subwidget. A minimal reproducible example is as follows.
main_window.py
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from subwidget import SubWidget
class Window(qtw.QWidget):
def __init__(self):
super().__init__()
# Configure Window
self.setWindowFlags(qtc.Qt.FramelessWindowHint)
self.setAttribute(qtc.Qt.WA_TranslucentBackground)
self.setFixedSize(350, 450)
# Init Methods
self.setupUI()
self.refresh_entries()
self.show()
def setupUI(self):
# Main Layout/Frame
self.main_layout = qtw.QHBoxLayout()
self.main_frame = qtw.QFrame()
self.main_frame_layout = qtw.QVBoxLayout()
# Set Layouts
self.setLayout(self.main_layout)
self.main_layout.addWidget(self.main_frame)
self.main_frame.setLayout(self.main_frame_layout)
# Configure QFrame
self.main_frame.setContentsMargins(10, 10, 10, 10)
self.main_frame.setObjectName("main_frame")
self.main_frame.setStyleSheet("""
border: None;
border-radius: 10px;
background: #1E1E1E;
""")
# Secondary Layout/Widget
self.main_scroll_area = qtw.QScrollArea()
self.container_widget = qtw.QWidget()
self.container_layout = qtw.QVBoxLayout()
self.main_scroll_area.setObjectName("main_scroll_area")
self.main_scroll_area.setWidgetResizable(True)
self.main_scroll_area.setVerticalScrollBarPolicy(qtc.Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
self.main_scroll_area.setHorizontalScrollBarPolicy(qtc.Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
self.main_scroll_area.setWidget(self.container_widget)
self.container_widget.setLayout(self.container_layout)
self.container_widget.setObjectName("container_widget")
# Widget -> Layout
self.main_frame_layout.addWidget(self.main_scroll_area, 0, qtc.Qt.AlignmentFlag.AlignHCenter | qtc.Qt.AlignmentFlag.AlignCenter)
def refresh_entries(self):
self.clear_entries()
for i in range(5):
self.container_layout.addWidget(SubWidget(i ** i ** i, i))
def clear_entries(self):
for i in reversed(range(self.container_layout.count())):
try:
widget = self.container_layout.itemAt(i).widget()
widget.setParent(None)
except Exception:
pass
if __name__ == "__main__":
app = qtw.QApplication(sys.argv)
win = Window()
sys.exit(app.exec())
and subwidget.py
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
class SubWidget(qtw.QWidget):
def __init__(self, name, index):
super().__init__()
# Variables
self.name = str(name)
self.index = index
# Init Methods
self.setupUI()
self.show()
def setupUI(self):
# Main Layout/Frame
self.main_layout = qtw.QHBoxLayout()
self.main_frame = qtw.QFrame()
self.main_frame_layout = qtw.QVBoxLayout()
# Set Layouts
self.setLayout(self.main_layout)
self.main_layout.addWidget(self.main_frame)
self.main_frame.setLayout(self.main_frame_layout)
# Configure QFrame
self.main_frame.setContentsMargins(5, 5, 5, 5)
self.main_frame.setStyleSheet("""
border: None;
border-radius: 5px;
background: #000000;
""")
# Secondary Layout/Widget
self.name_lbl = qtw.QLabel()
# Configure Seondary Widgets
self.name_lbl.setText(self.name)
self.name_lbl.setStyleSheet("""
color: #FFFFFF;
""")
self.name_lbl.setWordWrap(True) # https://stackoverflow.com/questions/49838817/how-to-resize-qlabels-to-fit-contents-in-qscrollarea
# Widget -> Layout
self.main_frame_layout.addWidget(self.name_lbl, 0, qtc.Qt.AlignmentFlag.AlignHCenter | qtc.Qt.AlignmentFlag.AlignCenter)
This is a visual of the resulting window/frame.
A Wrapable Anywhere QLabel
My way to achieve this to subclass QLabel and and implement the paintEvent, where you can set the text alignment to TextWrapAnywhere when you drawItemText.
Here is a sample code.
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QStyleOption, QVBoxLayout, QWidget, QStyle
class SuperQLabel(QLabel):
def __init__(self, *args, **kwargs):
super(SuperQLabel, self).__init__(*args, **kwargs)
self.textalignment = Qt.AlignLeft | Qt.TextWrapAnywhere
self.isTextLabel = True
self.align = None
def paintEvent(self, event):
opt = QStyleOption()
opt.initFrom(self)
painter = QPainter(self)
self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)
self.style().drawItemText(painter, self.rect(),
self.textalignment, self.palette(), True, self.text())
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setFixedSize(100, 200)
self.label = QLabel()
self.label.setWordWrap(True)
self.label.setText("1111111111111111111111111111")
self.slabel = SuperQLabel()
self.slabel.setText("111111111111111111111111111")
self.centralwidget = QWidget()
self.setCentralWidget(self.centralwidget)
self.mainlayout = QVBoxLayout()
self.mainlayout.addWidget(self.label)
self.mainlayout.addWidget(self.slabel)
self.centralwidget.setLayout(self.mainlayout)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
The result.
Note: this work is tested with pyqt5, pyside2, pyside6 and should work in qt5 as well (in c++).

Resizing QGraphicsScene when layout changes [duplicate]

This question already has an answer here:
Coordinates of an image PyQt
(1 answer)
Closed 2 years ago.
I'm using a QGraphicsScene in PySide2, and I want to resize its contents when the scene resizes. I'm able to trigger the resize when the main window resizes, but I can't figure out how to trigger it when the contents of the window change.
Here's a small example where I have a graphics scene and two push buttons. In the scene is a circle that should just touch the edges. When I click one of the buttons, it shows or hides the other button.
import sys
from PySide2.QtGui import QResizeEvent
from PySide2.QtWidgets import (QPushButton, QApplication, QVBoxLayout, QDialog,
QSizePolicy, QGraphicsScene, QGraphicsView)
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
# Create widgets
self.scene = QGraphicsScene()
self.circle = self.scene.addEllipse(0, 0, 10, 10)
self.extra = QGraphicsView(self.scene)
self.extra.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
self.button1 = QPushButton("Button 1")
self.button2 = QPushButton("Button 2")
# Create layout and add widgets
layout = QVBoxLayout()
layout.addWidget(self.extra)
layout.addWidget(self.button1)
layout.addWidget(self.button2)
# Set dialog layout
self.setLayout(layout)
# Add button signal to greetings slot
self.button1.clicked.connect(self.on_click1)
self.button2.clicked.connect(self.on_click2)
def on_click1(self):
self.button2.setVisible(not self.button2.isVisible())
self.resizeEvent()
def on_click2(self):
self.button1.setVisible(not self.button1.isVisible())
self.resizeEvent()
def resizeEvent(self, event: QResizeEvent = None):
size = self.extra.maximumViewportSize()
target_size = min(size.width(), size.height()) - 1
self.circle.setRect((size.width() - target_size) // 2,
(size.height() - target_size) // 2,
target_size,
target_size)
self.extra.scene().setSceneRect(0, 0, size.width(), size.height())
if __name__ == '__main__':
# Create the Qt Application
app = QApplication(sys.argv)
# Create and show the form
form = Form()
form.show()
# Run the main Qt loop
sys.exit(app.exec_())
The scene is resized when the main window resizes and when one of the buttons disappears or reappears. The circle adjusts its size when the main window resizes, but not when the buttons change.
How can I handle the resizing when the buttons change? I'd like to keep the circle just touching the edge of the display as it resizes.
The problem was that I was handling the resize of the main window, and not the graphics view. Create a new subclass of QGraphicsView, and handle the resize event there. It will catch both kinds of resize event.
import sys
from PySide2.QtGui import QResizeEvent
from PySide2.QtWidgets import (QPushButton, QApplication, QVBoxLayout, QDialog,
QSizePolicy, QGraphicsScene, QGraphicsView, QWidget)
class ResizingView(QGraphicsView):
def __init__(self, parent: QWidget = None):
super().__init__(parent)
self.setScene(QGraphicsScene())
self.circle = self.scene().addEllipse(0, 0, 1, 1)
def resizeEvent(self, event: QResizeEvent):
super().resizeEvent(event)
size = event.size()
target_size = min(size.width(), size.height()) - 1
x = (size.width() - target_size) // 2
y = (size.height() - target_size) // 2
self.circle.setRect(x, y, target_size, target_size)
self.setSceneRect(0, 0, size.width(), size.height())
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
# Create widgets
self.extra = ResizingView()
self.extra.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
self.button1 = QPushButton("Button 1")
self.button2 = QPushButton("Button 2")
# Create layout and add widgets
layout = QVBoxLayout()
layout.addWidget(self.extra)
layout.addWidget(self.button1)
layout.addWidget(self.button2)
# Set dialog layout
self.setLayout(layout)
# Add button signal to greetings slot
self.button1.clicked.connect(self.on_click1)
self.button2.clicked.connect(self.on_click2)
def on_click1(self):
self.button2.setVisible(not self.button2.isVisible())
def on_click2(self):
self.button1.setVisible(not self.button1.isVisible())
if __name__ == '__main__':
# Create the Qt Application
app = QApplication(sys.argv)
# Create and show the form
form = Form()
form.show()
# Run the main Qt loop
sys.exit(app.exec_())
If you don't want to move all the resize logic into the graphics view, create a new signal on your subclass, and connect it to a slot on whatever class should handle the resize event. If this still isn't working for you, check that you're calling setSceneRect() on the graphics view.
If you don't like recalculating the sizes of everything, you may prefer the simpler approach of just scaling everything with the fitInView() method. It's certainly easier, but I prefer the finer control you can have with a full set of size calculations. Here's an example of using fitInView().
import sys
from PySide2.QtCore import Qt
from PySide2.QtGui import QResizeEvent, QColor
from PySide2.QtWidgets import (QPushButton, QApplication, QVBoxLayout, QDialog,
QSizePolicy, QGraphicsScene, QGraphicsView, QWidget)
class ResizingView(QGraphicsView):
def __init__(self, parent: QWidget = None):
super().__init__(scene=QGraphicsScene(), parent=parent)
self.circle = self.scene().addEllipse(1, 1, 98, 98)
self.scene().addSimpleText('Centre').setPos(50, 50)
self.scene().setBackgroundBrush(QColor('green'))
self.scene().setSceneRect(0, 0, 100, 100)
def resizeEvent(self, event: QResizeEvent):
self.fitInView(0, 0, 100, 100, Qt.KeepAspectRatio)
super().resizeEvent(event)
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
# Create widgets
self.extra = ResizingView()
self.extra.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
self.button1 = QPushButton("Button 1")
self.button2 = QPushButton("Button 2")
# Create layout and add widgets
layout = QVBoxLayout()
layout.addWidget(self.extra)
layout.addWidget(self.button1)
layout.addWidget(self.button2)
# Set dialog layout
self.setLayout(layout)
# Add button signal to greetings slot
self.button1.clicked.connect(self.on_click1)
self.button2.clicked.connect(self.on_click2)
def on_click1(self):
self.button2.setVisible(not self.button2.isVisible())
def on_click2(self):
self.button1.setVisible(not self.button1.isVisible())
if __name__ == '__main__':
# Create the Qt Application
app = QApplication(sys.argv)
# Create and show the form
form = Form()
form.show()
# Run the main Qt loop
sys.exit(app.exec_())
Either way, the key thing is to subclass QGraphicsView and handle its resizeEvent() instead of the one on the main form.

PyQt5 window not opening on show() when parent window is specified

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

PyQt5 - Passed reference is None

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))

Categories