Not able to display Qlabel on PyQt4

1.8k views Asked by At

Hi i have posted the code below through which i'm unable to display label in pyqt4. Any suggestions would be helpful .

from PyQt4 import QtGui
from PyQt4 import QtCore
import sys

class Entry_view(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setGeometry(25, 25, 800, 480)

        label = QtGui.QLabel()
        label.setText("Welcome To Python GUI")
        label.resize(100, 50)
        # label.show(self)
        self.show()


if __name__ == '__main__':
app     = QtGui.QApplication(sys.argv)
myapp = Entry_view()
sys.exit(app.exec_())
3

There are 3 answers

0
Siddharth Savant On BEST ANSWER

code below is the solution ,

from PyQt4 import QtGui
from PyQt4 import QtCore
import sys

class Entry_view(QtGui.QWidget):
def __init__(self, parent=None):
    QtGui.QWidget.__init__(self, parent)
    self.setGeometry(25, 25, 800, 480)

    label = QtGui.QLabel()
    label.setText("Swipe The Card")
    vbox = QtGui.QVBoxLayout()
    label.setAlignment(Qt.AlignCenter)
    vbox.addWidget(label)
    vbox.addStretch()
    self.setLayout(vbox)

if __name__ == '__main__':
app     = QtGui.QApplication(sys.argv)
myapp = Entry_view()
sys.exit(app.exec_())
0
ekhumoro On

You did not keep a reference to the label, so it got garbage-collected before it could be shown. Try this instead:

self.label = QtGui.QLabel(self)
self.label.setText("Welcome To Python GUI")
self.label.resize(100, 50)
0
Jaidee On

Why did you set the text but don't process the app using thus:

app.processEvents() # On the QApplication.

Or just do:

label = QtGui.QLabel(text="Welcome to Python GUI!")

Or:

label = QtGui.QLabel("Welcomme To Python GUI!")

Or another way is:

label.show() # No widgets on it.