pyqt5之控件大乱斗

# -*- 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('

?

', self) self.label_1.setAlignment(Qt.AlignCenter) self.label_1.setGeometry(100, 300, 150, 50) self.label_2 = QLabel('

...

', 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('

intrusion...

') self.label_2.setText('

WARNING

') self.label_1.setPalette(self.my_color_red) self.label_2.setPalette(self.my_color_red) self.textEdit_1.append("illegal people") elif n % 5 == 0: self.label_1.setText('

intrusion...

') self.label_2.setText('

WARNING

') self.label_1.setPalette(self.my_color_green) self.label_2.setPalette(self.my_color_green) self.textEdit_1.append("legal people") else: self.label_1.setText('

no people

') self.label_2.setText('

...

') self.label_1.setPalette(self.my_color_black) self.label_2.setPalette(self.my_color_black) self.textEdit_1.append("no people") 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('

...

') 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_())


我曾经跨过山和大海,也穿过人山人海,我曾经拥有着的一切,转眼都飘散如烟,我曾经失落失望失掉所有方向,直到看见平凡才是唯一的答案。
——韩寒《平凡之路》

 

你可能感兴趣的:(pyqt5)