PyQt is not showing Button if i set window to show

2019-08-23 03:07发布

问题:

PyQt is not showing Button if i set window to showMaximize()

If i set a self.setGeometry(50, 50, 500, 300) then the Button is showing perfectly Facing issue at showMaximized()

import sys
from PyQt4 import QtGui, QtCore


class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.showMaximized()
        self.setWindowTitle("PyQT tuts!")
        self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))
        self.home()

    def home(self):
        btn = QtGui.QPushButton("Quit", self)
        btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        btn.resize(100, 100)
        btn.move(100, 100)
        self.show()


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


run()

Any Help Would be appreciated ,

I need to place the Button at center of my Window .

回答1:

The problem is that the children are shown by the parents, in your case when the parent is shown the button is not yet a child so it will not be shown, so there are 2 possible solutions:

  1. Set as a child before showMaximized()


    class Window(QtGui.QMainWindow):
        def __init__(self):
            super(Window, self).__init__()
            self.home()
            self.showMaximized()
            self.setWindowTitle("PyQT tuts!")
            self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))
    
  2. call the show method of the button.


    def home(self):
        btn = QtGui.QPushButton("Quit", self)
        btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        btn.resize(100, 100)
        btn.move(100, 100)
        btn.show()