如果我将 window 设置为 showMaximize(),PyQt 不会显示 Button

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

如果我将 window 设置为 showMaximize()

,PyQt 不显示按钮

如果我设置 self.setGeometry(50, 50, 500, 300) 然后 Button 在 showMaximized()

处完美显示 Facing issue
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()

任何帮助将不胜感激,

我需要将按钮放在 Window 的中心。

问题是 children 由 parent 显示,在您的情况下,当显示 parent 时,按钮还不是 child所以不会显示,所以有2种可能的解决方案:

  1. 设为child前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. 调用按钮的show方法


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