pyqt5学习5
实例1:电位器关联显示QLCDNumber, QDial
import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLCDNumber, QDial from PyQt5.QtGui import QIcon from PyQt5.QtCore import QCoreApplication from random import randint class Example(QWidget): def __init__(self): super().__init__() self.init_ui() def init_ui(self): self.setGeometry(1068, 612, 600, 600) self.setWindowTitle('天晟出品') self.setWindowIcon(QIcon('ComNet.ico')) self.lcd = QLCDNumber(self) self.dial = QDial(self) self.lcd.setGeometry(100, 50, 150, 60) self.dial.setGeometry(120, 120, 100, 100) self.dial.valueChanged.connect(self.lcd.display) self.qbtn = QPushButton('退出', self) #新建按钮对象 self.qbtn.clicked.connect(QCoreApplication.instance().quit) #按钮点击事件 self.qbtn.resize(100, 50) self.qbtn.move(450, 450) self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
实例2:上下左右按键显示
import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel from PyQt5.QtGui import QIcon from PyQt5.QtCore import QCoreApplication class Example(QWidget): def __init__(self): super().__init__() self.init_ui() def init_ui(self): self.setGeometry(1068, 612, 600, 600) self.setWindowTitle('天晟出品') self.setWindowIcon(QIcon('ComNet.ico')) qbtn = QPushButton('退出', self) #新建按钮对象 qbtn.clicked.connect(QCoreApplication.instance().quit) #按钮点击事件 qbtn.resize(100, 100) qbtn.move(450, 450) self.lab = QLabel('方向', self) self.lab.setGeometry(200, 200, 100, 100) self.lab.setFocus() self.show() 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('→') if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
实例3:以点划线。QPainter, QColor, QPen
import sys from PyQt5.QtWidgets import (QApplication, QWidget, QLabel) from PyQt5.QtGui import QIcon, QPainter, QColor, QPen from PyQt5.QtCore import QCoreApplication, Qt class Example(QWidget): distance_from_center = 0 def __init__(self): super().__init__() self.init_ui() self.setMouseTracking(True) # 默认情况下禁用鼠标跟踪, 如果启用鼠标跟踪,即使没有按钮被按下,小部件也会接收鼠 # 标移动事件。当然你也可以不写,只需要在执行的过程中按照鼠标左键也行。 def init_ui(self): self.setGeometry(1068, 612, 600, 600) self.setWindowTitle('天晟出品') self.setWindowIcon(QIcon('ComNet.ico')) self.lab = QLabel(self) self.lab.setGeometry(0, 0, 600, 40) self.pos = None self.show() def mouseMoveEvent(self, event): # 这个函数就是捕捉鼠标移动事件了,我们把得 # 到的坐标已经一些相关的信息显示在label上。必须调用函数update()才能更新图形。 distance_from_center = round(((event.y() - 250) ** 2 + (event.x() - 500) ** 2) ** 0.5) self.lab.setText('坐标: ( x: %d ,y: %d )' % (event.x(), event.y()) + " 离中心点距离: " + str(distance_from_center)) self.pos = event.pos() self.update() def paintEvent(self, event): # 绘图的话需要重写绘图事件,我们生成QPainter对象,然后调用 # drawLine()方法绘制一条线,需要四个参数,起点的坐标,终点的坐标 if self.pos: q = QPainter(self) q.drawLine(0, 0, self.pos.x(), self.pos.y()) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
实例4:有时,知道哪个窗口小部件是信号的发送者非常有用。 为此,PyQt5具有sender()方法。例如下面这个例子,我们实现了简单的石头、剪刀、布的小游戏。
import sys from PyQt5.QtWidgets import (QApplication, QMessageBox, QWidget, QPushButton) from PyQt5.QtGui import QIcon from random import randint class Example(QWidget): distance_from_center = 0 def __init__(self): super().__init__() self.init_ui() def init_ui(self): self.setGeometry(1068, 612, 600, 600) self.setWindowTitle('天晟出品') self.setWindowIcon(QIcon('ComNet.ico')) 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.button_clicked) bt2.clicked.connect(self.button_clicked) bt3.clicked.connect(self.button_clicked) self.show() def button_clicked(self): computer = randint(1, 3) player = 0 sender = self.sender() if sender.text() == '剪刀': player = 1 elif sender.text() == '石头': player = 2 else: player = 3 if 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_())
实例5:从QObject创建的对象可以发出信号。 以下示例显示了我们如何发出自定义信号。
import sys from PyQt5.QtWidgets import (QApplication, QMessageBox, QWidget, QPushButton) from PyQt5.QtGui import QIcon from random import randint from PyQt5.QtCore import (pyqtSignal, QObject) class Signal(QObject): show_mouse = pyqtSignal() class Example(QWidget): def __init__(self): super().__init__() self.init_ui() def init_ui(self): self.setGeometry(1068, 612, 600, 600) self.setWindowTitle('天晟出品') self.setWindowIcon(QIcon('ComNet.ico')) self.s = Signal() self.s.show_mouse.connect(self.about) self.show() def about(self): QMessageBox.about(self, '鼠标', '你点鼠标了吧!') def mousePressEvent(self, e): self.s.show_mouse.emit() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())