我知道这个问题已经问过很多遍了,但是每次我看到不同的情况时。
第一个问题:
我无法打开具有网格布局的新窗口(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()
答案 0 :(得分: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_())