Sebiancoder
Sebiancoder

Reputation: 124

PyQt4 Label not showing

I am very new to PyQt4 and this question is probably very simple but I have tried many different things and nothing works. I am trying to make a label in PyQt4.

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

The label doesn't show up, and there is no error message. What am I doing wrong?

Upvotes: 0

Views: 2007

Answers (1)

Paul Cornelius
Paul Cornelius

Reputation: 10989

I use PySide and not Qt, but they are supposed to be 99.99% compatible. The main problem is your call to the show() function, which makes the window visible. You have two calls to show. The first time it is called, you have not yet called MainDisplay so the QLabel hasn't been created yet. The second time you call show the window is already visible, so nothing changes.

If you create the widgets first and call show once, at the end, it will work the way you want. With this code the label shows up.

There are other issues:

You will have to change the import statements back the way you had them.

I did not have your Time class, so I just wrote a simple piece of text into the label.

The function ShowFullScreen should be showFullScreen.

The function that runs the event loop in QtApp is named exec_ not 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_())

Upvotes: 1

Related Questions