代码之家  ›  专栏  ›  技术社区  ›  Sebiancoder

PyQt4标签未显示

  •  0
  • Sebiancoder  · 技术社区  · 8 年前

    import sys
    from PyQt4 import QtCore
    from PyQt4 import QtGui
    
    class Display(QtGui.QWidget):
        def __init__(self):
             super(Display, self).__init__()
             self.time = Time()       #Another class in the program
             self.ShowFullScreen()
             self.setStyleSheet("background-color: black;")
             self.show()
             self.MainDisplay()
    
        def MainDisplay(self):
            self.timedisplay = QtGui.QLabel(self)
            self.timedisplay.setStyleSheet("font: 30pt Helvetica; color: white;")
            self.timedisplay.setText(time.GetTime())
            self.show()
    
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        GUI = Display()
        sys.exit(app.exec())
    

    1 回复  |  直到 8 年前
        1
  •  1
  •   Paul Cornelius    8 年前

    我使用PySide而不是Qt,但它们应该99.99%兼容。主要问题是你打电话给 show() show . 第一次呼叫时,您尚未呼叫 MainDisplay 所以 QLabel 显示 窗户是 可见,因此没有任何变化。

    如果您首先创建小部件并调用 显示 最后,它会按你想要的方式工作。有了这个代码,标签就出现了。

    您必须将导入语句更改回原来的方式。

    函数 ShowFullScreen 应该是 showFullScreen

    在QtApp中运行事件循环的函数名为 exec_ exec .

    import sys
    from PySide import QtCore
    from PySide import QtGui
    
    class Display(QtGui.QWidget):
        def __init__(self):
             super(Display, self).__init__()
             self.setStyleSheet("background-color: black;")
             self.MainDisplay()
             self.showFullScreen()
    
        def MainDisplay(self):
            self.timedisplay = QtGui.QLabel(self)
            self.timedisplay.setStyleSheet("font: 30pt Helvetica; color: white;")
            self.timedisplay.setText("What time is it now?")
    
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        GUI = Display()
        sys.exit(app.exec_())