PyQt5按钮单击事件未调用连接方法

时间:2020-08-16 11:05:11

标签: python qt pyqt pyqt5

以下是我的应用程序中的第二个窗口,第一个窗口中的按钮全部正常工作,代码几乎相同。当我从窗口1启动它或单独启动它时,第二个窗口中的按钮不起作用。

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

编辑: 在尝试了一些不同的解决方案之后,我决定创建一个使用相同代码的新窗口,唯一的区别是没有文本传递给新窗口。在此新创建的窗口上,按钮起作用。我认为按钮问题来自将字符串“ Error Msg”(存储在变量label_txt中)传递给窗口。

Edit2:

.ui文件:

<?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>

编辑3:

当我删除样式表时,它可以工作。这很奇怪,因为所有其他窗口/按钮都可以与样式表一起使用

1 个答案:

答案 0 :(得分:0)

问题与我使用的样式表有关。将我的PyQt5降级到5.14版可以解决此问题。 @musicamante非常感谢您在这里的帮助!

相关问题