2016-04-04 104 views
0

我試圖將文本添加到樣式框中我有這裏,不能爲我的生活弄清楚。我知道這很簡單。我已閱讀文檔,但仍無法掌握它。我在這裏做錯了什麼?將文本添加到窗口小部件pyqt和python代碼

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() 
+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_() 
相關問題