目錄
PyQt5筆記(01) – 創建空白窗體
PyQt5筆記(02) – 按鈕點擊事件
PyQt5筆記(03) – 消息框
PyQt5筆記(04) – 文本框的使用
PyQt5筆記(05) – 絕對位置
為了便於后期更新,所有目錄已匯總到一個鏈接,具體請移步到這里
正文
本節主要介紹在一個PyQt窗體內添加一個按鈕,鼠標停留在按鈕上會出現提示,並在點擊按鈕時響應一個事件
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QWidget):
def __init__(self):
super().__init__()
self.title = "PyQt5 button"
self.left = 10
self.top = 10
self.width = 320
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
"""在窗體內創建button對象"""
button = QPushButton("PyQt5 Button", self)
"""方法setToolTip在用戶將鼠標停留在按鈕上時顯示的消息"""
button.setToolTip("This is an example button")
"""按鈕坐標x = 100, y = 70"""
button.move(100, 70)
"""按鈕與鼠標點擊事件相關聯"""
button.clicked.connect(self.on_click)
self.show()
"""創建鼠標點擊事件"""
@pyqtSlot()
def on_click(self):
print("PyQt5 button click")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
