Python Forum

Full Version: Popup window not coming
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi,
i am new to PyQt5. i am trying to make a GUI with the help of a tutorial. but popup window is not coming when i click the button

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import  QMessageBox
import sys

class Ui_ClickWindow(object):
    def setupUi(self, ClickWindow):
        ClickWindow.setObjectName("ClickWindow")
        ClickWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(ClickWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.button = QtWidgets.QPushButton(self.centralwidget)
        self.button.setGeometry(QtCore.QRect(70, 90, 641, 341))
        self.button.setIconSize(QtCore.QSize(40, 40))
        self.button.setObjectName("button")
        ClickWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(ClickWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        ClickWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(ClickWindow)
        self.statusbar.setObjectName("statusbar")
        ClickWindow.setStatusBar(self.statusbar)

        self.retranslateUi(ClickWindow)
        QtCore.QMetaObject.connectSlotsByName(ClickWindow)

        self.button.clicked.connect(self.show_popup)
        
    def retranslateUi(self, ClickWindow):
        _translate = QtCore.QCoreApplication.translate
        ClickWindow.setWindowTitle(_translate("ClickWindow", "Click me"))
        self.button.setText(_translate("ClickWindow", "Press me"))

    def show_popup(self):
        msg = QMessageBox()
        msg.setWindowTitle("Lame")
        msg.setText("your computer is Lame. Thank you")
        msg.setIcon(QMessageBox.Warning)
        msg.standardButtons(QMessageBox.Ok|QMessageBox.Abort|QMessageBox.Ignore)
        x = msg.exec_()
        
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ClickWindow = QtWidgets.QMainWindow()
    ui = Ui_ClickWindow()
    ui.setupUi(ClickWindow)
    ClickWindow.show()
    sys.exit(app.exec_())
in line 39 try

msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Abort | QMessageBox.Ignore)
The issue might be with the button connection. Add a print() inside show_popup() to confirm if the method is triggered. If it’s not triggered, check your button’s signal connection.

Here’s the modified version:

def show_popup(self):
    print("Button clicked!")  # Check if triggered
    msg = QMessageBox()
    msg.setWindowTitle("Lame")
    msg.setText("your computer is Lame. Thank you")
    msg.setIcon(QMessageBox.Warning)
    msg.exec_()
If the print shows but no popup appears, check your environment or installation. If no print appears, verify the signal-slot connection.