使用 Qt Designer 在 PySide2 中实现 signal/slots 时出错
Error while implementing signal/slots in PySide2 using Qt Designer
我使用 Qt Designer 设计了一个用户界面,并使用 signal/slot 编辑器添加了 signal/slots。我的 UI 中有 pushButton,信号是 clicked(),slot 是 click()。 pushButton 是我使用 QT Designer 创建的一个小部件。使用 python 3.7 和 QT 设计器 5.12
QT Designer 自动生成以下代码:
from PySide2 import QtCore, QtGui, QtWidgets
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
self.pushButton = QtWidgets.QPushButton(self.frame_1)
self.pushButton.setObjectName("pushButton")
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.pushButton.click)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
我在 class UI_mainWindow()
中添加了以下代码以执行 pushbutton.click
功能。但是我收到错误。请帮我解决这个问题。我需要在我的代码中添加按钮点击功能
class pushButton():
def click(self):
print("Button is clicked")
.ui QT Designer生成的文件:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>56</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>18</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<receiver>pushButton</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>107</x>
<y>76</y>
</hint>
<hint type="destinationlabel">
<x>107</x>
<y>76</y>
</hint>
</hints>
</connection>
</connections>
</ui>
错误:
Process finished with exit code -1073741571 (0xC00000FD)
根据您的初始逻辑,您正在创建一个无限循环,因为如果您发出点击信号,例如根据您的逻辑按下按钮,则还将调用同一按钮的 click() 方法,该方法也会发出点击信号将回调同一个按钮的 click() 方法等的信号,永远不会结束。
相反,将点击信号连接到 window:
获取以下内容。ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>56</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>52</x>
<y>59</y>
</hint>
<hint type="destinationlabel">
<x>283</x>
<y>201</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>click()</slot>
</slots>
</ui>
您必须使用以下命令转换为 .py:
pyside2-uic design.ui -o design.py -x
design.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'design.ui',
# licensing of 'design.ui' applies.
#
# Created: Sun Aug 11 08:07:08 2019
# by: pyside2-uic running on PySide2 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PySide2 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(20, 20, 56, 17))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), MainWindow.click)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtWidgets.QApplication.translate("MainWindow", "MainWindow", None, -1))
self.pushButton.setText(QtWidgets.QApplication.translate("MainWindow", "PushButton", None, -1))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
此外,如果您检查 .py,您会发现消息:WARNING! All changes made in this file will be lost!
,因此您必须创建另一个实现逻辑的 .py 文件,而不是在 design.py 中编写逻辑:
main.py
from PySide2 import QtCore, QtGui, QtWidgets
from design import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
@QtCore.Slot()
def click(self):
print("Button is clicked")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
├── design.py
└── main.py
我使用 Qt Designer 设计了一个用户界面,并使用 signal/slot 编辑器添加了 signal/slots。我的 UI 中有 pushButton,信号是 clicked(),slot 是 click()。 pushButton 是我使用 QT Designer 创建的一个小部件。使用 python 3.7 和 QT 设计器 5.12
QT Designer 自动生成以下代码:
from PySide2 import QtCore, QtGui, QtWidgets
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
self.pushButton = QtWidgets.QPushButton(self.frame_1)
self.pushButton.setObjectName("pushButton")
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.pushButton.click)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
我在 class UI_mainWindow()
中添加了以下代码以执行 pushbutton.click
功能。但是我收到错误。请帮我解决这个问题。我需要在我的代码中添加按钮点击功能
class pushButton():
def click(self):
print("Button is clicked")
.ui QT Designer生成的文件:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>56</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>18</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<receiver>pushButton</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>107</x>
<y>76</y>
</hint>
<hint type="destinationlabel">
<x>107</x>
<y>76</y>
</hint>
</hints>
</connection>
</connections>
</ui>
错误:
Process finished with exit code -1073741571 (0xC00000FD)
根据您的初始逻辑,您正在创建一个无限循环,因为如果您发出点击信号,例如根据您的逻辑按下按钮,则还将调用同一按钮的 click() 方法,该方法也会发出点击信号将回调同一个按钮的 click() 方法等的信号,永远不会结束。
相反,将点击信号连接到 window:
获取以下内容。ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>56</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>52</x>
<y>59</y>
</hint>
<hint type="destinationlabel">
<x>283</x>
<y>201</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>click()</slot>
</slots>
</ui>
您必须使用以下命令转换为 .py:
pyside2-uic design.ui -o design.py -x
design.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'design.ui',
# licensing of 'design.ui' applies.
#
# Created: Sun Aug 11 08:07:08 2019
# by: pyside2-uic running on PySide2 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PySide2 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(20, 20, 56, 17))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), MainWindow.click)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtWidgets.QApplication.translate("MainWindow", "MainWindow", None, -1))
self.pushButton.setText(QtWidgets.QApplication.translate("MainWindow", "PushButton", None, -1))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
此外,如果您检查 .py,您会发现消息:WARNING! All changes made in this file will be lost!
,因此您必须创建另一个实现逻辑的 .py 文件,而不是在 design.py 中编写逻辑:
main.py
from PySide2 import QtCore, QtGui, QtWidgets
from design import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
@QtCore.Slot()
def click(self):
print("Button is clicked")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
├── design.py
└── main.py