将文本添加到小部件pyqt和python代码

时间:2016-04-04 15:16:55

标签: python styles pyqt4

我试图将文字添加到我在这里的样式框中,但是不能为我的生活搞清楚。我知道这很简单。我已经阅读了文档,但仍然无法掌握它。我在这做错了什么?

class Weather(QtGui.QWidget):

    def __init__(self):
        super(Weather, self).__init__()

        self.initUI()

    def initUI(self):      

        hbox = QtGui.QHBoxLayout(self)

        topleft = QtGui.QFrame(self)
        topleft.setFrameShape(QtGui.QFrame.Panel)

        topright = QtGui.QFrame(self)
        topright.setFrameShape(QtGui.QFrame.Panel)

        bottom = QtGui.QFrame(self)
        bottom.setFrameShape(QtGui.QFrame.Panel)

        splitter1 = QtGui.QSplitter(QtCore.Qt.Horizontal)
        splitter1.addWidget(topleft)
        splitter1.addWidget(topright)
        splitter1.addWidget(QtGui.QLabel('Humidity:{:0.1f}%'.format(humidity) ))
        splitter1.addWidget(QtGui.QLabel('Temp:{:0.1f} F'.format(temperature) ))

        splitter2 = QtGui.QSplitter(QtCore.Qt.Vertical)
        splitter2.addWidget(splitter1)
        splitter2.addWidget(bottom)



        hbox.addWidget(splitter2)
        self.setLayout(hbox)
        QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))


        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Weather')
        self.show()

1 个答案:

答案 0 :(得分:0)

我遗漏的是定义结果,因为你可以看到我在这里做了。然后将变量放在我希望它出现在脚本和GUI中的位置。

from PyQt4.QtCore import Qt
    from PyQt4.QtGui import QWidget, QApplication, QSplitter, QLabel, QVBoxLayout
    import Adafruit_DHT


    sensor_args = { '11': Adafruit_DHT.DHT11,
                    '22': Adafruit_DHT.DHT22,
                    '2302': Adafruit_DHT.AM2302 }

    humidity, temperature = Adafruit_DHT.read_retry(11, 4)

    temp = 'Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(temperature, humidity)

    temperature = temperature * 9/5.0 + 32
    class MyWidget(QWidget):
        def __init__( self, parent = None ):
            super(MyWidget, self).__init__(parent)

            self.setMinimumWidth(300) 
            self.setMinimumHeight(300)

            # create widgets
            a = QLabel('Humidity:{:0.1f}%'.format(humidity),self )
            b = QLabel('Temperature:{:0.1f}%'.format(temperature),self )
            c = QLabel('Sports', self)
            d = QLabel('News', self)

            for lbl in (a, b, c, d):
                lbl.setAlignment(Qt.AlignCenter)

            # create 2 horizontal splitters
            h_splitter1 = QSplitter(Qt.Horizontal, self)
            h_splitter1.addWidget(a)
            h_splitter1.addWidget(b)


            h_splitter2 = QSplitter(Qt.Horizontal, self)
            h_splitter2.addWidget(c)
            h_splitter2.addWidget(d)

            h_splitter1.splitterMoved.connect(self.moveSplitter)
            h_splitter2.splitterMoved.connect(self.moveSplitter)

            self._spltA = h_splitter1
            self._spltB = h_splitter2

            # create a vertical splitter
            v_splitter = QSplitter(Qt.Vertical, self)
            v_splitter.addWidget(h_splitter1)
            v_splitter.addWidget(h_splitter2)

            layout = QVBoxLayout()
            layout.addWidget(v_splitter)
            self.setLayout(layout)

        def moveSplitter( self, index, pos ):
            splt = self._spltA if self.sender() == self._spltB else self._spltB
            splt.blockSignals(True)
            splt.moveSplitter(index, pos)
            splt.blockSignals(False)

    if ( __name__ == '__main__' ):
        app = QApplication([])
        widget = MyWidget()
        widget.show()
        app.exec_()