pyqt5之控制元件大亂鬥
阿新 • • 發佈:2018-12-12
# -*- coding: utf-8 -*- import sys import time from PyQt5.QtWidgets import QApplication, QPushButton, QLabel, QWidget, QMainWindow, QLineEdit, QMessageBox,\ QTextEdit from PyQt5.QtCore import QTimer, Qt from PyQt5.QtGui import QFont, QPalette import numpy as np import matplotlib.pyplot as plt n = 0 class MyDemo(QMainWindow): def __init__(self): super(MyDemo, self).__init__() self.init_ui() def init_ui(self): self.resize(600, 500) self.setWindowTitle('Demo') self.btn_1 = QPushButton('testing', self) self.btn_1.setGeometry(100, 200, 100, 50) self.btn_1.clicked.connect(self.push_btn_1) self.btn_2 = QPushButton('exit', self) self.btn_2.setGeometry(450, 200, 100, 50) self.btn_2.clicked.connect(self.push_btn_2) self.btn_3 = QPushButton('stop warning', self) self.btn_3.setGeometry(450, 100, 100, 50) self.btn_3.clicked.connect(self.push_btn_3) self.btn_4 = QPushButton('stop testing', self) self.btn_4.setGeometry(300, 100, 100, 50) self.btn_4.clicked.connect(self.push_btn_4) self.btn_5 = QPushButton('clear textEdit', self) self.btn_5.setGeometry(100, 400, 100, 50) self.btn_5.clicked.connect(self.push_btn_5) self.btn_6 = QPushButton('clear number', self) self.btn_6.setGeometry(300, 155, 100, 40) self.btn_6.clicked.connect(self.push_btn_6) self.label_1 = QLabel('<h1>?</h1>', self) self.label_1.setAlignment(Qt.AlignCenter) self.label_1.setGeometry(100, 300, 150, 50) self.label_2 = QLabel('<h1>...</h1>', self) self.label_2.setAlignment(Qt.AlignCenter) self.label_2.setGeometry(100, 100, 100, 50) self.linEdit_1 = QLineEdit('state', self) self.linEdit_1.setAlignment(Qt.AlignCenter) self.linEdit_1.setFont(QFont('', 16, QFont.Bold)) self.linEdit_1.setGeometry(220, 200, 200, 50) self.textEdit_1 = QTextEdit(self) self.textEdit_1.setGeometry(300, 270, 260, 200) self.mt = QTimer(self) self.mt.timeout.connect(self.my_function) self.my_color_red = QPalette() self.my_color_red.setColor(QPalette.WindowText, Qt.red) self.my_color_green = QPalette() self.my_color_green.setColor(QPalette.WindowText, Qt.green) self.my_color_black = QPalette() self.my_color_black.setColor(QPalette.WindowText, Qt.black) self.show() def my_function(self): global n try: if n % 2 == 0: self.label_1.setText('<h1>intrusion...</h1>') self.label_2.setText('<h1>WARNING</h1>') self.label_1.setPalette(self.my_color_red) self.label_2.setPalette(self.my_color_red) self.textEdit_1.append("<font color='red' size='14' >illegal people</font>") elif n % 5 == 0: self.label_1.setText('<h1>intrusion...</h1>') self.label_2.setText('<h1>WARNING</h1>') self.label_1.setPalette(self.my_color_green) self.label_2.setPalette(self.my_color_green) self.textEdit_1.append("<font color='green' size='14' >legal people</font>") else: self.label_1.setText('<h1>no people</h1>') self.label_2.setText('<h1>...</h1>') self.label_1.setPalette(self.my_color_black) self.label_2.setPalette(self.my_color_black) self.textEdit_1.append("<font color='black' size='14'>no people</font>") n += 1 self.linEdit_1.setText('number: %d' % n) except Exception as e: print(e) def push_btn_1(self): self.mt.start(200) # self.my_function() # self.mt.singleShot(1000, self.push_btn_1) def push_btn_2(self): self.close() def push_btn_3(self): self.label_2.setText('<h1>...</h1>') def push_btn_4(self): self.mt.stop() def push_btn_5(self): self.textEdit_1.clear() def push_btn_6(self): global n n = 0 def closeEvent(self, event): reply = QMessageBox.question(self, 'exit', 'Are you sure to exit?', QMessageBox.Yes | QMessageBox.Cancel) if reply == QMessageBox.Yes: event.accept() else: event.ignore() if __name__ == '__main__': app = QApplication(sys.argv) w = MyDemo() sys.exit(app.exec_())
我曾經跨過山和大海,也穿過人山人海,我曾經擁有著的一切,轉眼都飄散如煙,我曾經失落失望失掉所有方向,直到看見平凡才是唯一的答案。 ——韓寒《平凡之路》