Python 3.x 链接pyQt文件不';不相关
我有一个pyQt脚本(在终端中使用pyuic5创建),它创建了一个包含一些函数的窗口。这部作品和剧本如下:Python 3.x 链接pyQt文件不';不相关,python-3.x,menu,pyqt5,Python 3.x,Menu,Pyqt5,我有一个pyQt脚本(在终端中使用pyuic5创建),它创建了一个包含一些函数的窗口。这部作品和剧本如下: from PyQt5 import QtCore, QtGui, QtWidgets from Main import Ui_Main from resources.icons import * import sys class MainW(QtWidgets.QMainWindow): def __init__(self, parent=None): supe
from PyQt5 import QtCore, QtGui, QtWidgets
from Main import Ui_Main
from resources.icons import *
import sys
class MainW(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainW, self).__init__(parent) #pyQt5
self.ui = Ui_Main()
self.ui.setupUi(self)
self.ui.setupUi
self.ui.retranslateUi(self)
# self.ui.setWindowIcon(QtWidgets.QIcon('MeasLogoSm.png'))
# self.ui.setWindowTitle("Meas Sound Measurement Tool")
self.ui.progressBar.setProperty("value", 1)
# measExit = self.ui.menuJBae_Meas.addAction(self.ui.actionExit)
# measExit.triggered.connect(self.Exit)
def Exit(self):
choice = QtGui.QMessageBox.question(self, 'Exit Meas',
"Are You sure to Leave Meas?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
if choice == QtGui.QMessageBox.Yes:
sys.exit()
else:
pass
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainapp = MainW()
mainapp.show()
sys.exit(app.exec_())
问题在这之后出现了。我想创建一些函数。我从菜单中的退出功能开始。我无法将退出脚本链接到该脚本。我认为这是因为标准文件有一个对象
main
,其中所有子对象都与之相关。
我不知道如何将我的脚本(现在是上面的脚本)与pyuic5中的标准脚本(下面的脚本)联系起来
第二个脚本如何使用
main
中的对象?在Qt设计器中添加的所有小部件都成为分配给self.ui
的对象的属性,这只是一个简单的名称空间。因此,您可以访问actionExit
并将其连接到插槽,如下所示:
class MainW(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainW, self).__init__(parent)
self.ui = Ui_Main()
self.ui.setupUi(self)
# self.ui.setWindowIcon(QtWidgets.QIcon('MeasLogoSm.png'))
# self.ui.setWindowTitle("Meas Sound Measurement Tool")
self.ui.progressBar.setProperty("value", 1)
self.ui.actionExit.triggered.connect(self.Exit)
def Exit(self):
choice = QtWidgets.QMessageBox.question(self, 'Exit Meas',
"Are You sure to Leave Meas?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if choice == QtWidgets.QMessageBox.Yes:
self.close()
但是请注意,Exit
方法有一些错误,应该如下所示:
class MainW(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainW, self).__init__(parent)
self.ui = Ui_Main()
self.ui.setupUi(self)
# self.ui.setWindowIcon(QtWidgets.QIcon('MeasLogoSm.png'))
# self.ui.setWindowTitle("Meas Sound Measurement Tool")
self.ui.progressBar.setProperty("value", 1)
self.ui.actionExit.triggered.connect(self.Exit)
def Exit(self):
choice = QtWidgets.QMessageBox.question(self, 'Exit Meas',
"Are You sure to Leave Meas?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if choice == QtWidgets.QMessageBox.Yes:
self.close()
如果您不想一直键入self.ui
,可以使用多重继承方法简化一些事情:
class MainW(QtWidgets.QMainWindow, Ui_Main):
def __init__(self, parent=None):
super(MainW, self).__init__(parent)
self.setupUi(self)
# self.setWindowIcon(QtWidgets.QIcon('MeasLogoSm.png'))
# self.setWindowTitle("Meas Sound Measurement Tool")
self.progressBar.setProperty("value", 1)
self.actionExit.triggered.connect(self.Exit)
现在,在Qt Designer中添加的所有小部件都是
MainW
对象的直接属性。在Qt Designer中添加的所有小部件都成为分配给self.ui
的对象的属性,这只是一个简单的名称空间。因此,您可以访问actionExit
并将其连接到插槽,如下所示:
class MainW(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainW, self).__init__(parent)
self.ui = Ui_Main()
self.ui.setupUi(self)
# self.ui.setWindowIcon(QtWidgets.QIcon('MeasLogoSm.png'))
# self.ui.setWindowTitle("Meas Sound Measurement Tool")
self.ui.progressBar.setProperty("value", 1)
self.ui.actionExit.triggered.connect(self.Exit)
def Exit(self):
choice = QtWidgets.QMessageBox.question(self, 'Exit Meas',
"Are You sure to Leave Meas?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if choice == QtWidgets.QMessageBox.Yes:
self.close()
但是请注意,Exit
方法有一些错误,应该如下所示:
class MainW(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainW, self).__init__(parent)
self.ui = Ui_Main()
self.ui.setupUi(self)
# self.ui.setWindowIcon(QtWidgets.QIcon('MeasLogoSm.png'))
# self.ui.setWindowTitle("Meas Sound Measurement Tool")
self.ui.progressBar.setProperty("value", 1)
self.ui.actionExit.triggered.connect(self.Exit)
def Exit(self):
choice = QtWidgets.QMessageBox.question(self, 'Exit Meas',
"Are You sure to Leave Meas?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if choice == QtWidgets.QMessageBox.Yes:
self.close()
如果您不想一直键入self.ui
,可以使用多重继承方法简化一些事情:
class MainW(QtWidgets.QMainWindow, Ui_Main):
def __init__(self, parent=None):
super(MainW, self).__init__(parent)
self.setupUi(self)
# self.setWindowIcon(QtWidgets.QIcon('MeasLogoSm.png'))
# self.setWindowTitle("Meas Sound Measurement Tool")
self.progressBar.setProperty("value", 1)
self.actionExit.triggered.connect(self.Exit)
现在,在Qt Designer中添加的所有小部件都是
MainW
对象的直接属性。您所问的代码不是有效的python代码-它有语法错误,部分已被删除。始终确保发布重现问题所需的所有代码,并确保其正确运行。现在应该是有效代码问题中的代码不是有效的python代码-它有语法错误,部分已被删除。始终确保发布重现问题所需的所有代码,并确保其正确运行。现在它应该是有效的代码。您可以解释为什么self.close()
优于sys.exit()
?在pyqt中,使用sys.exit(app.exec_389;())
启动应用程序更惯用,然后用close()
或quit()
停止。但是Exit
方法中真正的错误是您使用的是QtGui
,而不是qtwidget
。现在检查一下,我知道您做了什么。。第一次就错过了!!你能解释一下为什么self.close()
比sys.exit()
好吗?在pyqt中,用sys.exit(app.exec())
启动应用程序,然后用close()
或quit()
停止应用程序更惯用。但是Exit
方法中真正的错误是您使用的是QtGui
,而不是qtwidget
。现在检查一下,我知道您做了什么。。第一次就错过了!!