close
一、前言
上一篇說到如何高速拉好UI介面,並給物件取名,而後讓python可以去執行
還不會的趕快花5分鐘去學一下 【Pyqt5使用者介面開發】Python使用Qt Designer快速拉好使用者介面並讓它活起來
這邊教大家如何用python Pyqt5高速的讓你的app加上你想要的基本功能
二、為你的app加上功能
未修改前程式碼(上一篇的程式碼)
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QFrame
import sys
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(454, 177)
self.pushButton_print = QtWidgets.QPushButton(Form)
self.pushButton_print.setGeometry(QtCore.QRect(170, 120, 93, 28))
self.pushButton_print.setObjectName("pushButton_print")
self.label_print = QtWidgets.QLabel(Form)
self.label_print.setGeometry(QtCore.QRect(20, 20, 51, 61))
self.label_print.setObjectName("label_print")
self.lineEdit_print = QtWidgets.QLineEdit(Form)
self.lineEdit_print.setGeometry(QtCore.QRect(80, 20, 351, 61))
self.lineEdit_print.setObjectName("lineEdit_print")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton_print.setText(_translate("Form", "點我開始"))
self.label_print.setText(_translate("Form", "顯示:"))
class MainFrame(QFrame, Ui_Form):
def __init__(self, parent=None):
super(MainFrame, self).__init__(parent) # 調用父類把子類對象轉為父類對象
# 調用介面
self.setupUi(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
mainFrame = MainFrame()
mainFrame.show()
sys.exit(app.exec_())
(1) 定義槽函數
在MainFrame class下加上自訂槽函數
class MainFrame(QFrame, Ui_Form):
def __init__(self, parent=None):
super(MainFrame, self).__init__(parent) # 調用父類把子類對象轉為父類對象
# 調用介面
self.setupUi(self)
# 自訂槽函數
def calculation(self):
a = 1
b = 2
c = a + b
d = c * 2
self.lineEdit_print.setText('恩哥好帥' + str(d)) # .setText方法讓文字顯示在lineEdit上
(2) 連接信號
用 .clicked.connect()來連接槽函數,其中clicked是點擊一下的意思
你也可以去找到其他事件
class MainFrame(QFrame, Ui_Form):
def __init__(self, parent=None):
super(MainFrame, self).__init__(parent) # 調用父類把子類對象轉為父類對象
# 調用介面
self.setupUi(self)
# 信號
self.pushButton_print.clicked.connect(self.calculation) # 信號與槽連接
# 自訂槽函數
def calculation(self):
a = 1
b = 2
c = a + b
d = c * 2
self.lineEdit_print.setText('恩哥好帥' + str(d)) # .setText方法讓文字顯示在lineEdit上
(3) 完整程式碼
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QFrame
import sys
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(454, 177)
self.pushButton_print = QtWidgets.QPushButton(Form)
self.pushButton_print.setGeometry(QtCore.QRect(170, 120, 93, 28))
self.pushButton_print.setObjectName("pushButton_print")
self.label_print = QtWidgets.QLabel(Form)
self.label_print.setGeometry(QtCore.QRect(20, 20, 51, 61))
self.label_print.setObjectName("label_print")
self.lineEdit_print = QtWidgets.QLineEdit(Form)
self.lineEdit_print.setGeometry(QtCore.QRect(80, 20, 351, 61))
self.lineEdit_print.setObjectName("lineEdit_print")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton_print.setText(_translate("Form", "點我開始"))
self.label_print.setText(_translate("Form", "顯示:"))
class MainFrame(QFrame, Ui_Form):
def __init__(self, parent=None):
super(MainFrame, self).__init__(parent) # 調用父類把子類對象轉為父類對象
# 調用介面
self.setupUi(self)
# 信號
self.pushButton_print.clicked.connect(self.calculation) # 信號與槽連接
# 自訂槽函數
def calculation(self):
a = 1
b = 2
c = a + b
d = c * 2
self.lineEdit_print.setText('恩哥好帥' + str(d)) # .setText方法讓文字顯示在lineEdit上
if __name__ == '__main__':
app = QApplication(sys.argv)
mainFrame = MainFrame()
mainFrame.show()
sys.exit(app.exec_())
這樣一來執行後就可以點按紐與介面有基本互動了~
三、後記
這個範例用了三個元件label(標籤)、pushButton(按鈕)、lineEdit(編輯列)
用了lineEdit的.setText方法,趕快去說明文件找到自己想用的元件與他的方法吧!
接下來看完這篇,更有效率的寫使用者介面喔喔喔喔喔!!!!
【Pyqt5使用者介面開發】物件導向概念結合Pyqt5:更有效率管理程式碼!
文章標籤
全站熱搜
留言列表