PyQt5初级——2
生活随笔
收集整理的這篇文章主要介紹了
PyQt5初级——2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
QLineEdit.selectAll()QLineEdit.setFocus()QLineEdit.text()QLineEdit.clear()self.text = QLineEdit('在這里輸入數字', self)self.text.selectAll()self.text.setFocus()self.text.setGeometry(80, 50, 150 ,30)
QMessageBox.about
QMessageBox.critical
QMessageBox.information
QMessageBox.warningQMessageBox.question(self, '確認', '確認退出嗎', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)critical = information = warning = questionQMessageBox.about(self, '看結果','猜小了!')
?事件:
點擊關閉按鈕會觸發closeEvent事件。
可以重寫該事件來達到相應的目的。
def closeEvent(self, event):reply = QMessageBox.question(self, '確認', '確認退出嗎', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)if reply == QMessageBox.Yes:event.accept() else:event.ignore()?鍵盤觸發事件
def keyPressEvent(self, e):if e.key() == Qt.Key_Up:self.lab.setText('↑')elif e.key() == Qt.Key_Down:self.lab.setText('↓')elif e.key() == Qt.Key_Left:self.lab.setText('←')else:self.lab.setText('→')繪畫事件
def paintEvent(self, event):if self.pos:q = QPainter(self)q.drawLine(0, 0, self.pos.x(), self.pos.y())?鼠標移動事件
def mouseMoveEvent(self, event):distance_from_center = round(((event.y() - 250)**2 + (event.x() - 500)**2)**0.5)self.label.setText('坐標: ( x: %d ,y: %d )' % (event.x(), event.y()) + " 離中心點距離: " + str(distance_from_center)) self.pos = event.pos()self.update() self.setMouseTracking(True) #開啟鼠標跟蹤?有時,知道哪個窗口小部件是信號的發送者非常有用。 為此,PyQt5具有sender()方法。
import sys from PyQt5.QtWidgets import (QApplication, QMessageBox, QWidget, QPushButton) from random import randintclass Example(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):self.setGeometry(200, 200, 300, 300)self.setWindowTitle('學點編程吧')bt1 = QPushButton('剪刀',self)bt1.setGeometry(30,180,50,50)bt2 = QPushButton('石頭',self)bt2.setGeometry(100,180,50,50)bt3 = QPushButton('布',self)bt3.setGeometry(170,180,50,50)bt1.clicked.connect(self.buttonclicked)bt2.clicked.connect(self.buttonclicked)bt3.clicked.connect(self.buttonclicked)self.show()def buttonclicked(self):computer = randint(1,3)player = 0sender = self.sender()if sender.text() == '剪刀':player = 1elif sender.text() == '石頭':player = 2else:player = 3if player == computer:QMessageBox.about(self, '結果', '平手')elif player == 1 and computer == 2:QMessageBox.about(self, '結果', '電腦:石頭,電腦贏了!')elif player == 2 and computer == 3:QMessageBox.about(self, '結果', '電腦:布,電腦贏了!')elif player == 3 and computer == 1:QMessageBox.about(self,'結果','電腦:剪刀,電腦贏了!')elif computer == 1 and player == 2:QMessageBox.about(self,'結果','電腦:剪刀,玩家贏了!')elif computer == 2 and player == 3:QMessageBox.about(self,'結果','電腦:石頭,玩家贏了!')elif computer == 3 and player == 1:QMessageBox.about(self,'結果','電腦:布,玩家贏了!')if __name__ == '__main__':app = QApplication(sys.argv)ex = Example()sys.exit(app.exec_())信號?
發出自定義信號
import sys from PyQt5.QtWidgets import (QApplication, QWidget, QMessageBox) from PyQt5.QtCore import (pyqtSignal, QObject)class Signal(QObject):showmouse = pyqtSignal()class Example(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):self.setGeometry(200, 200, 300, 300)self.setWindowTitle('學點編程吧')self.s = Signal()self.s.showmouse.connect(self.about)self.show()def about(self):QMessageBox.about(self,'鼠標','你點鼠標了吧!')def mousePressEvent(self, e):self.s.showmouse.emit()if __name__ == '__main__':app = QApplication(sys.argv)ex = Example()sys.exit(app.exec_())布局?
QHBoxLayout:水平方向布局管理器
QVBoxLayout:垂直方向布局管理器
QGridLayout:網格布局管理器
bt1 = QPushButton('剪刀', self)bt2 = QPushButton('石頭', self)bt3 = QPushButton('布', self)hbox = QHBoxLayout()hbox.addStretch(1) #增加伸縮量hbox.addWidget(bt1)hbox.addStretch(1)#增加伸縮量hbox.addWidget(bt2)hbox.addStretch(1)#增加伸縮量hbox.addWidget(bt3)hbox.addStretch(6)#增加伸縮量self.setLayout(hbox)或者也可以直接
hbox = QHBoxLayout(self);這樣就不需要self.setLayout(hbox)了。?
其中四個addStretch()函數用于在button按鈕間增加伸縮量,伸縮量的比例為1:1:1:6,意思就是將button以外的空白地方按設定的比例等分為9份并按照設定的順序放入布局器中。
?
總結
以上是生活随笔為你收集整理的PyQt5初级——2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PyQt5初级——
- 下一篇: PyQt5基础——3