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

问题描述 投票:1回答:1

我知道这个问题已被多次询问,但每次我看到不同的情况。

第一个问题:

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

我试图在pyqt中打开一个新窗口(Window2),这个窗口(Window2)有网格布局。

为了使网格布局工作,Window2有父(QWidget)

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

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

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

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

作为父母,Window2打开但没有网格布局

并且我不知道如何在仍然具有网格布局的情况下正确打开窗口

编辑:我发现有关多重继承的问题,但我无法理解它是如何工作的Multiple inheritance

第二个问题:

我有一个全局变量numberofholes,其值在类“Window”中更改,然后在类“Window2”中使用

所以这个变量在类“Window”中正确更改,但是未定义或者它的值在类“Window2”中没有改变,那么如何在“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()
python pyqt grid global new-window
1个回答
0
投票

试试吧:

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.