【发布时间】:2020-12-05 17:13:46
【问题描述】:
以下是我的应用程序中的第二个窗口,我的第一个窗口中的按钮都可以工作,代码几乎相同。第二个窗口中的按钮在我从窗口 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 中)传递给窗口。
编辑2:
.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:
当我删除样式表时它可以工作。这很奇怪,因为所有其他窗口/按钮都使用样式表
【问题讨论】:
-
能否也分享一下
ui文件? -
在编辑 2 中添加
-
我无法重现错误:它会打印消息并正确关闭窗口。你确定你指向正确的 ui 文件路径吗?
-
是的,文件路径正确,窗口打开并显示来自 label_txt 的消息,只有按钮不起作用。是否有不同的方式将参数(字符串)传递给 UI ?
-
那肯定是qdarkstyle相关的bug。这里似乎有一个相关的问题:Qt 5.15 MacOS - No reaction on Button-Click