简体   繁体   中英

PyQt5 Button clicked event not calling connected method

The following is the second window in my app, the buttons in my first window all work, code is almost the same. The button in second window doesn't work both when I launch it from window 1 and when I launch it on its own.

    import sys
    import os
    from PyQt5.QtWidgets import QMainWindow, QApplication
    from PyQt5 import uic
    import qdarkstyle
    
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    
    qtCreatorFile = "PATH TO MY .ui FILE" 
    
    Ui_error, QtBaseClass = uic.loadUiType(qtCreatorFile) #process through pyuic
    
    class MyApp1(QMainWindow, Ui_error): #gui class
        def __init__(self,label_txt):
            #The following sets up the gui via Qt
            super(MyApp1, self).__init__()
            self.ui = Ui_error()
            self.ui.setupUi(self)
    
            #set up callbacks
            self.ui.label.setText(label_txt)
            self.ui.label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
            self.ui.label.setAlignment(Qt.AlignCenter)
            
            self.ui.b_err.clicked.connect(self.close_w) 

#This button doesn't work
# Tried to use self.close directly in clicked.connect()(which worked in window 1) but also doesn't work
    
        # Tried to use @staticmethod but didn't work
        def close_w(self):
            print('tst') # tst is not printed in console when button is clicked
            self.close()
    
    
    
    
    def errorGUI(label_txt):
        app = QApplication(sys.argv) 
        ###
        app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5()) # Issue was caused by the stylesheet
        ###
        window = MyApp1(label_txt)
        window.show()
        sys.exit(app.exec_())
    
    
    if __name__ == "__main__":
        errorGUI("Error Msg")

Edit: After trying some different solutions I decided to create a new window which uses the same code with the only difference being that no text is passed to the new window. On this newly created window the button works. I assume the button problem comes from passing the string "Error Msg"(stored in variable label_txt) to the window.

Edit2:

.ui file:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>error</class>
 <widget class="QMainWindow" name="error">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>493</width>
    <height>210</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Error</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QLabel" name="label">
      <property name="enabled">
       <bool>true</bool>
      </property>
      <property name="sizePolicy">
       <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="font">
       <font>
        <pointsize>41</pointsize>
       </font>
      </property>
      <property name="text">
       <string>Placeholder Button</string>
      </property>
     </widget>
    </item>
    <item row="1" column="0">
     <widget class="QPushButton" name="b_err">
      <property name="font">
       <font>
        <pointsize>16</pointsize>
       </font>
      </property>
      <property name="text">
       <string>Close</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>493</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

Edit 3:

It works when I remove the stylesheet. This is strange because all other windows/buttons work with the stylesheet

The Issue was related to the stylesheet I was using. Downgrading my PyQt5 to version 5.14 fixed the issue. @musicamante thank you very much for your help here!

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