PyQt按钮不显示

时间:2018-09-27 01:07:35

标签: python python-3.x pyqt pyqt4

所以......我正在使用python的PyQt库创建一个Graphics类,该类抽象了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()

如果您能提供关于发生这种情况的原因的信息,那就太好了。谢谢

1 个答案:

答案 0 :(得分:1)

在Qt中,有一个基本规则:QWidget子项是相对于父项QWidget绘制的,如果没有父项,则这将是一个窗口,称为top-级别。

另一个概念是QPushButtonQLabelQSpinBox等,因为它们是从此类继承的,所以它们是QWidgets

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

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

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

enter image description here


如果您的意图是让某些QLabelQPushButton成为某些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_())

enter image description here


如果要在创建按钮后添加父项,则可以使用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_())

main.py

gui=Graphics()
w1 = gui.newWidget("hmm")
bt1 = gui.addButton("hey","hello")
bt1.setParent(w1) # <-- set w1 as parent of bt1
gui.start()
相关问题