Python PyQt按钮不显示

Python PyQt按钮不显示,python,python-3.x,pyqt,pyqt4,Python,Python 3.x,Pyqt,Pyqt4,因此……我正在使用PyQt库for python创建一个图形类,该类抽象掉了QtGui类的大部分功能。稍后我将在我的其他项目中使用它。这似乎工作得很好,除了按钮和其他小部件没有显示,尽管创建了窗口 import sys from PyQt4 import QtGui class Graphics: def __init__(self): self.app=QtGui.QApplication(sys.argv) self.widgets={}

因此……我正在使用PyQt库for python创建一个图形类,该类抽象掉了QtGui类的大部分功能。稍后我将在我的其他项目中使用它。这似乎工作得很好,除了按钮和其他小部件没有显示,尽管创建了窗口

import sys
from PyQt4 import QtGui

class Graphics:
    def __init__(self):

        self.app=QtGui.QApplication(sys.argv)
        self.widgets={}
        self.labels={}
        self.buttons={}


    def getApp(self):

        return self.app


    def newWidget(self,name:str):

        self.widgets[name]=QtGui.QWidget()
        return self.widgets[name]       


    def addButton(self,name:str,text:str):

        self.buttons[name]=QtGui.QPushButton(text)
        return self.buttons[name]


    def addLabel(self,name:str,text:str):

        self.labels[name]=QtGui.QLabel()
        self.labels[name].setText(text)
        return self.labels[name]


    def start(self):
        for widget in self.widgets:
            self.widgets[widget].show()
        sys.exit(self.app.exec_())
^这就是代码。下面展示了我如何实现这个类

from graphics import Graphics

gui=Graphics()
w1=gui.newWidget("hmm")
bt1=gui.addButton("hey","hello")
print(bt1)
gui.start()

如果您能提供有关发生这种情况的原因的见解,那就太好了。谢谢您在Qt中有一个基本规则:
QWidget
子项是相对于父项
QWidget
绘制的,如果它没有父项,这将是一个窗口,称为顶级

另一个概念是
QPushButton
QLabel
QSpinBox
等。它们是
QWidgets
,因为它们继承自此类

因此,由于
QPushButton
没有父项,它应该显示为一个窗口,为此,您应该使用
show()


如果您打算让某些
QLabel
QPushButton
成为某些
QWidget
的一部分,那么我们必须将该小部件指定为父部件,例如,在我的下一个解决方案中,我建议添加小部件的名称,如果小部件不存在,则应创建:

import sys
from PyQt4 import QtGui

class Graphics:
    def __init__(self):
        self.app=QtGui.QApplication(sys.argv)
        self.widgets={}
        self.labels={}
        self.buttons={}

    def getApp(self):
        return self.app

    def newWidget(self, name:str):
        w = QtGui.QWidget()
        self.widgets[name] = w
        return w     

    def addButton(self, widget_name:str, name:str, text:str):
        if widget_name in self.widgets:
            w = self.widgets[widget_name]
        else:
            w = self.newWidget(widget_name)
        button = QtGui.QPushButton(text, parent=w)
        self.buttons[name] = button
        return button

    def addLabel(self, widget_name:str, name:str, text:str):
        if widget_name in self.widgets:
            w = self.widgets[widget_name]
        else:
            w = self.newWidget(widget_name)
        label = QtGui.QLabel(text, parent=w)
        self.labels[name] = label
        return label

    def start(self):
        [w.show() for name, w in self.widgets.items()]

        sys.exit(self.app.exec_())


如果要在创建按钮后添加父级,则可以使用
setParent()

graphics.py

import sys
from PyQt4 import QtGui

class Graphics:
    def __init__(self):
        self.app=QtGui.QApplication(sys.argv)
        self.widgets={}
        self.labels={}
        self.buttons={}

    def getApp(self):
        return self.app

    def newWidget(self, name:str):
        w = QtGui.QWidget()
        self.widgets[name] = w
        return w     

    def addButton(self, name:str, text:str):
        button = QtGui.QPushButton(text)
        self.buttons[name] = button
        return button

    def addLabel(self, name:str, text:str):
        label = QtGui.QLabel(text)
        self.labels[name] = label
        return label

    def start(self):
        for _, w in in self.widgets.items():
            w.show()
        sys.exit(self.app.exec_())
gui=Graphics()
w1 = gui.newWidget("hmm")
bt1 = gui.addButton("hey","hello")
bt1.setParent(w1) # <-- set w1 as parent of bt1
gui.start()
main.py

import sys
from PyQt4 import QtGui

class Graphics:
    def __init__(self):
        self.app=QtGui.QApplication(sys.argv)
        self.widgets={}
        self.labels={}
        self.buttons={}

    def getApp(self):
        return self.app

    def newWidget(self, name:str):
        w = QtGui.QWidget()
        self.widgets[name] = w
        return w     

    def addButton(self, name:str, text:str):
        button = QtGui.QPushButton(text)
        self.buttons[name] = button
        return button

    def addLabel(self, name:str, text:str):
        label = QtGui.QLabel(text)
        self.labels[name] = label
        return label

    def start(self):
        for _, w in in self.widgets.items():
            w.show()
        sys.exit(self.app.exec_())
gui=Graphics()
w1 = gui.newWidget("hmm")
bt1 = gui.addButton("hey","hello")
bt1.setParent(w1) # <-- set w1 as parent of bt1
gui.start()
gui=Graphics()
w1=gui.newWidget(“hmm”)
bt1=gui.addButton(“嘿”,“你好”)

bt1.setParent(w1)#您希望按钮显示在小部件中还是单独显示?如果有两个小部件,并且添加了一个按钮,则该按钮应属于哪个小部件?是否有方法将按钮添加到某个小部件?我查阅了PyQt教程,他们只使用了一个小部件,因此我无法进一步了解,因此我问您是否已经添加了两个小部件使用newWidget()然后您想添加一个带有addButton()的按钮,该按钮应该显示在第一个或第二个小部件中的哪个位置?在代码中添加了两行:-对于self.buttons中的按钮:self.buttons[button].show()中的按钮还有一个问题,我如何将小部件作为父类添加到我的按钮中。再次感谢。这非常好!这就是我添加小部件作为父类的方式。这对我帮助很大。但是我可以添加我最初作为父类创建的同一小部件吗?@user7657046那么您是否可以创建对newWidget()的调用?@user7657046如果只创建一个小部件,那么为什么要创建self.widgets字典?