如果我将窗口设置为 showMaximize(),PyQt 不会显示按钮 [英] PyQt is not showing Button if i set window to showMaximize()

查看:40
本文介绍了如果我将窗口设置为 showMaximize(),PyQt 不会显示按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将窗口设置为 showMaximize(),PyQt 不会显示按钮

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

如果我设置了一个 self.setGeometry(50, 50, 500, 300)然后按钮在 showMaximized() 处显示完美的 Facing 问题

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

任何帮助将不胜感激,

我需要将 Button 放在我的 Window 的中心.

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

推荐答案

问题是孩子是由父母显示的,在你的情况下,当父母被显示时按钮还不是孩子所以它不会被显示,所以有两种可能的解决方案:

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. 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'))

  • 调用按钮的show方法.

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

  • 这篇关于如果我将窗口设置为 showMaximize(),PyQt 不会显示按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆