在 main 上而不是在 pyqt5 的脚本上实现按钮的功能
Posted
技术标签:
【中文标题】在 main 上而不是在 pyqt5 的脚本上实现按钮的功能【英文标题】:implementing buttons' functions on main instead on script from pyqt5 【发布时间】:2019-12-15 14:58:11 【问题描述】:我使用 Qt 设计器创建了一个 GUI,并将其转换为 .py 文件。 我曾经直接在 GUI .py 文件中编写函数,但有人告诉我将代码分成两个文件:一个只包含从 Qt 设计器转换的 GUI 命令,另一个是 Main - 这将适用于所有GUI 的按钮和小部件。
现在,我已经设法从不同的文件导入 GUI,当我运行脚本时,GUI 确实出现了,但是当我尝试编写函数时,我似乎遇到了麻烦。
这是我的主要内容:
from PyQt5 import QtWidgets
from mainwin import Ui_MainWindow
import sys
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.exitbtn.clicked.connect(self.exitclicked) ***this is the connection and function that I'm trying to make 'em work***
def exitclicked(self,Dialog):
sys.exit()
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
application = ApplicationWindow()
application.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
我不知道它是否相关,但这是我从 Qt 设计器转换而来的 GUI。
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(895, 422)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.txtoutput = QtWidgets.QTextBrowser(self.centralwidget)
self.txtoutput.setGeometry(QtCore.QRect(150, 200, 471, 192))
self.txtoutput.setObjectName("txtoutput")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(50, 90, 69, 22))
self.comboBox.setObjectName("comboBox")
self.lbl = QtWidgets.QLabel(self.centralwidget)
self.lbl.setGeometry(QtCore.QRect(90, 30, 81, 16))
self.lbl.setObjectName("lbl")
self.lcdNumber = QtWidgets.QLCDNumber(self.centralwidget)
self.lcdNumber.setGeometry(QtCore.QRect(340, 170, 151, 23))
self.lcdNumber.setObjectName("lcdNumber")
self.getserialbtn = QtWidgets.QPushButton(self.centralwidget)
self.getserialbtn.setGeometry(QtCore.QRect(210, 50, 75, 23))
self.getserialbtn.setObjectName("getserialbtn")
self.chuckrealeasebtn = QtWidgets.QPushButton(self.centralwidget)
self.chuckrealeasebtn.setGeometry(QtCore.QRect(50, 120, 75, 23))
self.chuckrealeasebtn.setObjectName("chuckrealeasebtn")
self.runbtn = QtWidgets.QPushButton(self.centralwidget)
self.runbtn.setGeometry(QtCore.QRect(580, 70, 91, 61))
font = QtGui.QFont()
font.setPointSize(28)
font.setBold(True)
font.setWeight(75)
self.runbtn.setFont(font)
self.runbtn.setCheckable(False)
self.runbtn.setObjectName("runbtn")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(50, 50, 151, 31))
self.textEdit.setObjectName("textEdit")
self.stopbtn = QtWidgets.QPushButton(self.centralwidget)
self.stopbtn.setGeometry(QtCore.QRect(680, 70, 91, 61))
font = QtGui.QFont()
font.setPointSize(28)
font.setBold(True)
font.setWeight(75)
self.stopbtn.setFont(font)
self.stopbtn.setObjectName("stopbtn")
self.exitbtn = QtWidgets.QPushButton(self.centralwidget)
self.exitbtn.setGeometry(QtCore.QRect(740, 370, 75, 23))
self.exitbtn.setObjectName("exitbtn")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.lbl.setText(_translate("MainWindow", "Enter serial #"))
self.getserialbtn.setText(_translate("MainWindow", "Enter"))
self.chuckrealeasebtn.setText(_translate("MainWindow", "chuck"))
self.runbtn.setText(_translate("MainWindow", "Run"))
self.stopbtn.setText(_translate("MainWindow", "Stop"))
self.exitbtn.setText(_translate("MainWindow", "Exit"))
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_())
我尝试运行它时遇到的错误是:
AttributeError: 'ApplicationWindow' object has no attribute 'exitbtn'
如果有人可以编写正确的语法以能够通过 main 实现功能,我会很高兴。
非常感谢:D
【问题讨论】:
将self.exitbtn.clicked.connect(self.exitclicked)
更改为self.ui.exitbtn.clicked.connect(self.exitclicked)
和def exitclicked(self, Dialog):
更改为def exitclicked(self):
嘿尼克!正如你所说,我改变了我的主要,但我仍然得到同样的错误..你还有什么想法可以让它工作吗?
【参考方案1】:
试试看:
main.py
from PyQt5 import QtWidgets
from mainwin import Ui_MainWindow
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# self.exitbtn.clicked.connect(self.exitclicked)
self.ui.exitbtn.clicked.connect(self.exitclicked) # +
def exitclicked(self): # +
self.close() # +
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
application = ApplicationWindow()
application.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
mainwin.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(895, 422)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.txtoutput = QtWidgets.QTextBrowser(self.centralwidget)
self.txtoutput.setGeometry(QtCore.QRect(150, 200, 471, 192))
self.txtoutput.setObjectName("txtoutput")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(50, 90, 69, 22))
self.comboBox.setObjectName("comboBox")
self.lbl = QtWidgets.QLabel(self.centralwidget)
self.lbl.setGeometry(QtCore.QRect(90, 30, 81, 16))
self.lbl.setObjectName("lbl")
self.lcdNumber = QtWidgets.QLCDNumber(self.centralwidget)
self.lcdNumber.setGeometry(QtCore.QRect(340, 170, 151, 23))
self.lcdNumber.setObjectName("lcdNumber")
self.getserialbtn = QtWidgets.QPushButton(self.centralwidget)
self.getserialbtn.setGeometry(QtCore.QRect(210, 50, 75, 23))
self.getserialbtn.setObjectName("getserialbtn")
self.chuckrealeasebtn = QtWidgets.QPushButton(self.centralwidget)
self.chuckrealeasebtn.setGeometry(QtCore.QRect(50, 120, 75, 23))
self.chuckrealeasebtn.setObjectName("chuckrealeasebtn")
self.runbtn = QtWidgets.QPushButton(self.centralwidget)
self.runbtn.setGeometry(QtCore.QRect(580, 70, 91, 61))
font = QtGui.QFont()
font.setPointSize(28)
font.setBold(True)
font.setWeight(75)
self.runbtn.setFont(font)
self.runbtn.setCheckable(False)
self.runbtn.setObjectName("runbtn")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(50, 50, 151, 31))
self.textEdit.setObjectName("textEdit")
self.stopbtn = QtWidgets.QPushButton(self.centralwidget)
self.stopbtn.setGeometry(QtCore.QRect(680, 70, 91, 61))
font = QtGui.QFont()
font.setPointSize(28)
font.setBold(True)
font.setWeight(75)
self.stopbtn.setFont(font)
self.stopbtn.setObjectName("stopbtn")
self.exitbtn = QtWidgets.QPushButton(self.centralwidget)
self.exitbtn.setGeometry(QtCore.QRect(740, 370, 75, 23))
self.exitbtn.setObjectName("exitbtn")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.lbl.setText(_translate("MainWindow", "Enter serial #"))
self.getserialbtn.setText(_translate("MainWindow", "Enter"))
self.chuckrealeasebtn.setText(_translate("MainWindow", "chuck"))
self.runbtn.setText(_translate("MainWindow", "Run"))
self.stopbtn.setText(_translate("MainWindow", "Stop"))
self.exitbtn.setText(_translate("MainWindow", "Exit"))
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_())
【讨论】:
以上是关于在 main 上而不是在 pyqt5 的脚本上实现按钮的功能的主要内容,如果未能解决你的问题,请参考以下文章
elasticsearch地理距离过滤器,但距离在文档上而不是在查询中
IOS5 中的本地通知 - 如何让它在屏幕上而不是在通知中心弹出?
Kendo UI Grid:可以允许在指定列上而不是在其他列上进行分组