简体   繁体   中英

PyQt5 program does not displaying the widgets

I was writing a pyqt5 program for expression evaluator but after running the program i am not able to see any widgets and getting blank window

def expressionevaluator():  
    import sys
    from PyQt5 import QtWidgets
    from PyQt5 import QtCore
    from PyQt5 import QtGui

    from PyQt5.QtWidgets import QApplication, QWidget,QMainWindow


    class Window(QtWidgets.QMainWindow):
        def __init__(self):
            super(Window,self).__init__()
            self.setGeometry(50,50,500,300)
            self.setWindowTitle("PyQt Tutorial")
            self.setWindowIcon=QtGui.QIcon('pyqt_example2.PNG')
            self.home()


        def ExitForm(self):
            sys.exit()

        def home(self):
            vbox=QtWidgets.QVBoxLayout()
            textbrowser=QtWidgets.QTextBrowser()
            lineedit=QtWidgets.QLineEdit()
            btn=QtWidgets.QPushButton("QUIT")
            btn.clicked.connect(self.close)
            vbox.addWidget(textbrowser)
            vbox.addWidget(lineedit)
            vbox.addWidget(btn)
            self.setLayout(vbox)
            self.show()


    if __name__=="__main__":        
        app=QApplication(sys.argv)
        GUI=Window()
        sys.exit(app.exec_())
expressionevaluator()

So what should I do?

Just running your code I got a widget showing up in my screen, but its components didn't show up. Instead of setting a layout of a QMainWindow try to have a central widget (QWidget) set its layout with its components than set the QMainWindow central widget with this widget. There you go, now you have all working fine.


You had problems with the layout because QMainWindow behaves differently from others Widgets, it has its own layout and many other default behaviors, central widget is the reason why nothing was showing up inside your main window.

def expressionevaluator():
    import sys

    from PyQt5.QtGui import QIcon
    from PyQt5.QtWidgets import QApplication
    from PyQt5.QtWidgets import QLineEdit
    from PyQt5.QtWidgets import QMainWindow
    from PyQt5.QtWidgets import QPushButton
    from PyQt5.QtWidgets import QTextBrowser
    from PyQt5.QtWidgets import QVBoxLayout
    from PyQt5.QtWidgets import QWidget



    class Window(QMainWindow):
        def __init__(self):
            super(Window,self).__init__()
            self.setGeometry(50,50,500,300)
            self.setWindowTitle("PyQt Tutorial")
            self.setWindowIcon = QIcon('pyqt_example2.PNG')
            self.home()


        def ExitForm(self):
            sys.exit()

        def home(self):
            vbox = QVBoxLayout()
            textbrowser = QTextBrowser()
            lineedit = QLineEdit()
            btn = QPushButton("QUIT")
            central_widget = QWidget()
            central_widget.setLayout(vbox)
            btn.clicked.connect(self.close)
            vbox.addWidget(textbrowser)
            vbox.addWidget(lineedit)
            vbox.addWidget(btn)
            self.setCentralWidget(central_widget)
            self.show()


    if __name__=="__main__":
        app = QApplication(sys.argv)
        GUI = Window()
        GUI.show()
        sys.exit(app.exec_())
expressionevaluator()

Note: There are many improvements in the structure of your code you could do, I just changed as less as I could to make it work, for example try to not import all the modules at once, import just what you need for example QIcon, QLineEdit and so on, instead of the whole QtWidgets, or QtCore...

Following code works well:

import sys



from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QTextBrowser
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget



class Window(QMainWindow):
    def __init__(self):
            super(Window,self).__init__()
            self.setGeometry(50,50,500,300)
            self.setWindowTitle("PyQt Tutorial")
            self.setWindowIcon = QIcon('pyqt_example2.PNG')
            self.home()

def ExitForm(self):
        sys.exit()

def home(self):
        vbox = QVBoxLayout()
        textbrowser = QTextBrowser()
        lineedit = QLineEdit()
        btn = QPushButton("QUIT")
        central_widget = QWidget()
        central_widget.setLayout(vbox)
        btn.clicked.connect(self.ExitForm)
        vbox.addWidget(textbrowser)
        vbox.addWidget(lineedit)
        vbox.addWidget(btn)
        self.setCentralWidget(central_widget)
        self.show()


if __name__=="__main__":
    app = QApplication(sys.argv)
    GUI = Window()
    GUI.show()
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM