简体   繁体   中英

show groupbox inside window (not in new window) PyQt5

My code show GroupBox in new window. how to return it to inside main windoow?

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

Try it:

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

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM