在 Pyqt5 中打开具有网格布局的新 Window

Opening new Window having grid layout in Pyqt5

我知道这个问题已经被问过很多次了,但每次我都看到不同的情况。

第一个问题:

我无法打开具有网格布局的新 window (Window2)。

我想在 pyqt 中打开一个新的 window (Window2),这个 window(Window2) 有网格布局。

为了使网格布局有效,Window2 有 parent(QWidget)

并使其打开 Window2 有另一个 parent(QMainWindow)

但是这两个 parent 相互冲突意味着:

仅将 QWidget 作为 parent,Window2 根本打不开

仅将 QMainWindow 作为 parent,Window2 打开但没有网格布局

将两者设为 parents 时,Window2 打开但没有网格布局

而且我不知道如何在仍然有网格布局的情况下正确打开 window

编辑:我发现了关于多重继承的问题,但我不明白它是如何工作的

第二题:

我有一个全局变量 numberofholes,它的值在 class "Window" 中更改,然后在 class "Window2"

中使用

因此此变量在 class"Window" 中正确更改,但未定义或其值在 class "Window2" 中未更改 那么如何在 class"Window2"

中定义全局值

部分代码:

import sys
from PyQt5 import QtWidgets, QtGui, QtCore

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

#######global variables#####################################
global memorysize
global numberofholes
####################################################################
class Window(QWidget):
    def __init__(self,parent=None):
        super(Window,self).__init__(parent)
        self.setWindowTitle("Memory")
        self.setGeometry(50,50,500,300)
        self.home()



    def home(self):

        self.grid=QGridLayout()
        self.setLayout(self.grid)

        self.memory=QLabel(self)
        self.memory.setText("Total Memory size")
        self.grid.addWidget(self.memory,0,0)

        self.memoryinput=QLineEdit(self)
        self.grid.addWidget(self.memoryinput,0,20)

        self.holes=QLabel(self)
        self.holes.setText("Number of holes")
        self.grid.addWidget(self.holes,5,0)

        self.inputholes=QLineEdit(self)
        self.grid.addWidget(self.inputholes,5,20)

        self.submit=QPushButton("OK",self)
        self.grid.addWidget(self.submit,10,0)

#################Action on clicking submit###########################
        self.submit.clicked.connect(self.getholes)
    def getholes(self):
        memorysize=float(self.memoryinput.text())
        numberofholes=int(self.inputholes.text())
        self.close()
        self.window2=Window2(self)

##############second window for holes input##########################
class Window2(QMainWindow,QWidget):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.setWindowTitle("Memory")
        self.setGeometry(50,50,500,300)
        self.home()
        self.show()
    def home(self):
        self.grid=QGridLayout()
        self.setLayout(self.grid)
        #print(numberofholes)

        for n in range (numberofholes):
            self.start_add=QLabel(self)
            self.start_add.setText("Starting Address")

            self.inputstart=QLineEdit(self)

            self.size=QLabel(self)
            self.size.setText("Size")

            self.inputsize=QLineEdit(self)

            self.grid.addWidget(self.start_add,2*n+1,0)
            self.grid.addWidget(self.inputstart,2*n+1,1)
            self.grid.addWidget(self.size,2*n+1,2)
            self.grid.addWidget(self.inputsize,2*n+1,3)

def main():
     app = QApplication(sys.argv)
     main = Window()
     main.show()
     sys.exit(app.exec_())


if __name__ == '__main__':
    main()

试一试:

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

### global variables 
# global memorysize                                          # ---
# global numberofholes                                       # ---
###        


class Window(QWidget):
    def __init__(self,parent=None):
        super(Window,self).__init__(parent)
        self.setWindowTitle("Memory 1")
        self.setGeometry(50, 50, 500, 300)
        self.home()

    def home(self):
        self.grid = QGridLayout()
        self.setLayout(self.grid)

        self.memory = QLabel(self)
        self.memory.setText("Total Memory size")
        self.grid.addWidget(self.memory, 0, 0)

        self.memoryinput = QLineEdit(self)
        self.grid.addWidget(self.memoryinput, 0, 20)

        self.holes = QLabel(self)
        self.holes.setText("Number of holes")
        self.grid.addWidget(self.holes, 5, 0)

        self.inputholes = QLineEdit(self)
        self.grid.addWidget(self.inputholes, 5, 20)

        self.submit = QPushButton("OK", self)
        self.grid.addWidget(self.submit, 10, 0)

        #       Action on clicking submit                 
        self.submit.clicked.connect(self.getholes)

    def getholes(self):
        memorysize    = float(self.memoryinput.text())
        numberofholes = int(self.inputholes.text())
        self.hide()                                             # --- close()
        self.window2 = Window2(memorysize, numberofholes)       # --- self
        self.window2.show()


#     second window for holes input    
class Window2(QWidget):                                         # --- QMainWindow,
    def __init__(self, memorysize, numberofholes, parent=None):
        super().__init__(parent)

        self.memorysize, self.numberofholes = memorysize, numberofholes
        print("memorysize=`{}`,\nnumberofholes=`{}`".format(self.memorysize, self.numberofholes))

        self.setWindowTitle("Memory 2")
        self.setGeometry(50,50,500,300)
        self.home()
        self.show()

    def home(self):
        self.grid = QGridLayout()
        self.setLayout(self.grid)
        print(self.numberofholes)

        for n in range (2):
            self.start_add = QLabel(self)
            self.start_add.setText("Starting Address")

            self.inputstart = QLineEdit(self)

            self.size = QLabel(self)
            self.size.setText("Size")

            self.inputsize = QLineEdit(self)

            self.grid.addWidget(self.start_add, 2*n+1, 0)
            self.grid.addWidget(self.inputstart,2*n+1, 1)
            self.grid.addWidget(self.size,      2*n+1, 2)
            self.grid.addWidget(self.inputsize, 2*n+1, 3)

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