The layout size setting for QstackLayout is not possible. What should I do? crying

Asked 2 years ago, Updated 2 years ago, 144 views

I inserted the background picture through palette in the poker game. But it doesn't work when you actually run a program... It was a code that worked normally when running as a single class, but if you add it to the widget through stack layout, the picture does not come out.(crying) Is there anything he's ideologically mistaken about? The class that was running as a single class was a P_main class.

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class Pwindow (QWidget): Used to implement the main window QstackedLayout inheriting #Qwidth
    def __init__(self):
        super(Pwindow,self).__init__()

        self.Stack=QStackedWidget()
        self.SU1=P_main()
        self.SU2=P_play()

        self.Stack.addWidget(self.SU1)#The first main screen to be output
        self.Stack.addWidget(self.SU2)#The screen that runs when the difficulty button is pressed
        self.layout=QGridLayout()

        self._init_ui()

    def _init_ui(self):
        self.resize(500,500)
        self.layout.addWidget(self.Stack)
        self.setLayout(self.layout)

class P_play(QWidget):

    def __init__(self):
        super().__init__()

        self._init_ui()

    def _init_ui(self):
        oimage = QImage("./white.JPG")

        palette = QPalette()
        palette.setBrush(10, QBrush(oimage))

        self.setPalette(palette)
        self.setWindowTitle ("I'm the only Indian poker!")

class P_main(QWidget):

    def __init__(self):
        super().__init__()

        self._init_ui()

    def _init_ui(self):
        oimage = QImage("./white.JPG")
        palette = QPalette()
        palette.setBrush(10, QBrush(oimage))

        btn1 = QPushButton ('Initial', self)
        btn2 = QPushButton ('intermediate', self)
        btn3 = QPushButton ('advanced', self)

        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(btn1)
        hbox.addStretch(1)
        hbox.addWidget(btn2)
        hbox.addStretch(1)
        hbox.addWidget(btn3)
        hbox.addStretch(1)

        hbox2 = QHBoxLayout()
        hbox2.addStretch(1)
        hbox2.addWidget(QLabel('I'm the only Indian poker')
        hbox2.addStretch(1)

        vbox = QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox2)
        vbox.addStretch(2)
        vbox.addLayout(hbox)
        vbox.addStretch(1)

        self.setLayout(vbox)
        self.setPalette(palette)
        self.setWindowTitle ("I'm the only Indian poker!")


if __name__ == '__main__':
    app=QApplication(sys.argv)
    ex=Pwindow()
    ex.show()
    sys.exit(app.exec_())

I'll also attach an image!

pyqt5 python3.6.1 pycharm

2022-09-22 19:08

2 Answers

import sys
from PyQt5.QtCore import pyqtSlot, Qt
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class MyWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.btn1 = QPushButton("Button1")
        self.btn1.clicked.connect(self.button_clicked)

        self.layout = QStackedLayout()
        self.layout.addWidget(P_play())
        self.layout.addWidget(self.btn1)
        self.setLayout(self.layout)

        self.layout.setCurrentIndex(1)

    @pyqtSlot()
    def button_clicked(self):
        self.layout.setCurrentIndex(0)

class P_play(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)

        self._init_ui()

    def _init_ui(self):
        self.setAutoFillBackground(True) #Add
        oimage = QImage("./white.jpg")

        palette = QPalette()
        palette.setBrush(10, QBrush(oimage))

        self.setPalette(palette)
        #self.setWindowTitle ("I'm the only Indian poker!")


class MyMain(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        win = MyWidget(self)
        # # table.setStyle(QStyleFactory.create('Fusion'))
        self.setCentralWidget(win)

        self.statusbar = self.statusBar()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyMain()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())


2022-09-22 19:08

What are you trying to do? You only uploaded a part of the code above I don't have an image.

Stack layout is literally a stack of widgets like stacks. Test the example below. When enabled, a button appears, and when you click the button, the table widget appears.

This means that widgets with buttons and widgets with tables are stacked, and you can expose what you want to expose.

It's hard to explain in detail because there's no question about what the purpose.

import sys
from PyQt5.QtCore import pyqtSlot, Qt
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class MyWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.btn1 = QPushButton("Button1")
        self.btn1.clicked.connect(self.button_clicked)

        self.layout = QStackedLayout()
        self.layout.addWidget(MyTable())
        self.layout.addWidget(self.btn1)
        self.setLayout(self.layout)

        self.layout.setCurrentIndex(1) #Button1visible

    @pyqtSlot()
    def button_clicked(self):
        self.layout.setCurrentIndex(0) #tableWidgetView

class MyTable(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUI(parent)

    def setupUI(self, parent):
        self.vbox = QVBoxLayout()
        self.tableWidget = QTableWidget(parent)
        self.vbox.addWidget(self.tableWidget)

        #self.vbox.addStretch(1)
        self.grid = QGridLayout()
        self.vbox.addLayout(self.grid)

        self.setGeometry(800, 200, 300, 300)


        self.tableWidget.setMinimumSize(290, 290)
        self.tableWidget.setRowCount(2)
        self.tableWidget.setColumnCount(2)
        self.setTableWidgetData()

        self.tableWidget.cellClicked.connect(self.mycell_clicked)
        self.tableWidget.verticalHeader().sectionClicked.connect(self.myheader_clicked)

        self.setLayout(self.vbox)

    def setTableWidgetData(self):
        self.tableWidget.setHorizontalHeaderLabels(["aaaa", "bbbb"])

        self.tableWidget.setItem(0, 0, QTableWidgetItem("(0,0)"))
        self.tableWidget.setItem(0, 1, QTableWidgetItem("(0,1)"))
        self.tableWidget.setItem(1, 0, QTableWidgetItem("(1,0)"))
        self.tableWidget.setItem(1, 1, QTableWidgetItem("(1,1)"))

    @pyqtSlot(int, int)
    def mycell_clicked(self, row, col):
        QMessageBox.information(self, '{}, {}'.format(row, col), '{}, {}'.format(row, col))

    @pyqtSlot(int)
    def myheader_clicked(self, logicalIndex):
        QMessageBox.information(self, str(logicalIndex + 1), str(logicalIndex + 1))

class MyMain(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        win = MyWidget(self)
        # # table.setStyle(QStyleFactory.create('Fusion'))
        self.setCentralWidget(win)

        self.statusbar = self.statusBar()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyMain()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())


2022-09-22 19:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.