在窗口内显示组框(不在新窗口中)PyQt5

时间:2019-03-31 18:51:36

标签: python python-3.x pyqt pyqt5

我的代码在新窗口中显示GroupBox。如何将其返回到主窗口中?

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Window(QtWidgets.QMainWindow, QtWidgets.QDialog):

    def __init__(self):
        super(Window, self).__init__()
        V = app.desktop().screenGeometry()
        h = V.height()
        w = V.width()
        x = 1000
        y = 600
        self.setGeometry(h/4, w/20, x, y)
        self.setFixedSize(x, y)
        self.setWindowTitle('Main Window')
        self.home()

    def home(self):

        Window.tools_in_home(self)

        self.show()

    def tools_in_home(self):
        self.groupBox = QtWidgets.QGroupBox('GroupBox')
        self.groupBox.move(150, 50)
        self.groupBox.resize(800, 400)

        hBoxLayout = QtWidgets.QHBoxLayout()

        button1 = QtWidgets.QPushButton('Test', self)
        hBoxLayout.addWidget(button1)

        self.groupBox.setLayout(hBoxLayout)

        vBox = QtWidgets.QVBoxLayout()
        vBox.addWidget(self.groupBox)
        self.setLayout(vBox)
        self.groupBox.show()

def run():
    global app
    app = QtWidgets.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())
run()

1 个答案:

答案 0 :(得分:0)

尝试一下:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Window(QtWidgets.QMainWindow):                   #, QtWidgets.QDialog):
    def __init__(self):
        super(Window, self).__init__()

# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
        centralWidget = QtWidgets.QWidget()
        self.setCentralWidget(centralWidget)
        self.grid = QtWidgets.QGridLayout(centralWidget)        
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

        V = QtWidgets.QApplication.desktop().screenGeometry()
        h = V.height()
        w = V.width()
        x = 1000
        y = 600
        self.setGeometry(h/4, w/20, x, y)
        self.setFixedSize(x, y)
        self.setWindowTitle('Main Window')
        self.home()

    def home(self):
#        Window.tools_in_home(self)
        self.tools_in_home()
        self.show()

    def tools_in_home(self):
        self.groupBox = QtWidgets.QGroupBox('GroupBox')
        self.groupBox.move(150, 50)
        self.groupBox.resize(800, 400)

        hBoxLayout = QtWidgets.QHBoxLayout()     
        button1 = QtWidgets.QPushButton('Test', self)
        hBoxLayout.addWidget(button1)
        self.groupBox.setLayout(hBoxLayout)

#        vBox = QtWidgets.QVBoxLayout()
#        vBox.addWidget(self.groupBox)

        self.grid.addWidget(self.groupBox)                     #  +++

#        self.setLayout(vBox)
#        self.groupBox.show()

def run():
#    global app
    app = QtWidgets.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())
run()

enter image description here