当前位置: 动力学知识库 > 问答 > 编程问答 >

styles - Adding text to widget pyqt and python code

问题描述:

Im trying to add text into the styled boxes i have here and cannot for the life of me figure it out. I know this is prob very simple. I have read the docs but still cannot grasp it. What am i doing wrong here?

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

网友答案:

What i had left out was defining the result as you can see I have done that here. Then put the variable where I want it to appear in the script and in the 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_()
分享给朋友:
您可能感兴趣的文章:
随机阅读: