PyQT5 记录笔记
#字号字体 font: 10pt "Consolas"; #颜色 color: rgb(0, 85, 0); color:red; #边框线条 border:2px groove gray; #圆角 border-radius:10px; #文字与框边距 padding:4px; #背景颜色 background-color: yellow; #所选内容的颜色 selection-color: yellow; #所选内容背景颜色 selection-background-color: blue #代码 禁止拉伸窗口 Dialog.setFixedSize(1010,550) #代码 配置背景色 self.ui.label_2.setStyleSheet('background-color:green') #界面控件属性,鼠标原型,进入,按下 三种效果 QPushButton{ background-color:rgb(229, 241, 251); border-radius:10px; color: black; border:2px groove gray; border-radius: 10px; } QPushButton:hover{background-color:rgb(229, 0, 251); border-radius:10px; color: black; border:2px groove gray; border-radius: 10px; } QPushButton:pressed{ background-color:rgb(204, 228, 247); border-style: inset; border:2px groove gray; border-radius: 10px; } #在代码里配置圆角 背景色 线框 self.ui.label_2.setStyleSheet("border-radius:20px;" "background-color: rgb(0, 255, 0);" "border:2px groove gray;") #在函数下 时时刷新QT界面 QApplication.processEvents() #Qpushbotton样式表 https://blog.csdn.net/aiwangtingyun/article/details/94462976 #lineEdit,textEdit读写 self.ui.lineEdit.text() self.ui.textEdit.toPlainText() self.ui.lineEdit.setText('写') self.ui.textEdit.setText('写') #lineEdit文字显示居中,代码 self.ui.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
1.整理配置字
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# -*- coding: utf-8 -*- # @Time : 1/25/2022 # @Athor : WuHe # @File : RemoveTsMain.py import re import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog # from Removets import Ui_Dialog class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(1010, 550) Dialog.setFixedSize(1010,550) self.pushButton = QtWidgets.QPushButton(Dialog) self.pushButton.setGeometry(QtCore.QRect(900, 490, 100, 40)) self.pushButton.setStyleSheet("font: 9pt \"Consolas\"; \n" "color: rgb(255, 0, 0);\n" "border:2px groove gray;\n" "border-radius:8px;\n" "padding:4px;\n" "background-color: rgb(204, 204, 204);\n" "selection-color: yellow;\n" "selection-background-color: blue") self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(Dialog) self.pushButton_2.setGeometry(QtCore.QRect(780, 490, 100, 40)) self.pushButton_2.setStyleSheet("font: 10pt \"Consolas\"; \n" "color: rgb(0, 0, 127);\n" "border:1px groove gray;\n" "border-radius:8px;\n" "padding:0 4px;\n" "background-color: rgb(249, 255, 250);\n" "selection-color: yellow;\n" "selection-background-color: blue") self.pushButton_2.setObjectName("pushButton_2") self.lineEdit = QtWidgets.QLineEdit(Dialog) self.lineEdit.setGeometry(QtCore.QRect(20, 75, 861, 50)) self.lineEdit.setStyleSheet("font: 25 9pt \"Microsoft JhengHei UI Light\";\n" "color: rgb(0, 0, 127);\n" "border:1px groove gray;\n" "border-radius:5px;\n" "padding:4px;\n" "\n" "selection-color: yellow;\n" "selection-background-color: blue") self.lineEdit.setText("") self.lineEdit.setObjectName("lineEdit") self.textEdit = QtWidgets.QTextEdit(Dialog) self.textEdit.setGeometry(QtCore.QRect(20, 140, 861, 301)) self.textEdit.setStyleSheet(" \n" "font: 10pt \"SimSun\";\n" "color: rgb(0, 0, 127);\n" "border:1px groove gray;\n" "border-radius:5px;\n" "padding:4px;\n" "\n" "selection-color: yellow;\n" "selection-background-color: blue") self.textEdit.setObjectName("textEdit") self.pushButton_3 = QtWidgets.QPushButton(Dialog) self.pushButton_3.setGeometry(QtCore.QRect(900, 80, 100, 40)) self.pushButton_3.setStyleSheet("font: 9pt \"Consolas\"; \n" "color: rgb(0, 0, 127);\n" "border:1px groove gray;\n" "border-radius:8px;\n" "padding:0 4px;\n" "background-color: rgb(249, 255, 250);\n" "selection-color: yellow;\n" "selection-background-color: blue") self.pushButton_3.setObjectName("pushButton_3") self.label = QtWidgets.QLabel(Dialog) self.label.setGeometry(QtCore.QRect(280, 0, 652, 77)) self.label.setStyleSheet("font: 14pt \"Imprint MT Shadow\";") self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(Dialog) self.label_2.setGeometry(QtCore.QRect(20, 490, 220, 21)) font = QtGui.QFont() font.setPointSize(10) self.label_2.setFont(font) self.label_2.setText("") self.label_2.setObjectName("label_2") self.retranslateUi(Dialog) self.pushButton.clicked.connect(Dialog.close) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Sort TsMaster Config Byte Tool")) self.pushButton.setText(_translate("Dialog", "Exit")) self.pushButton_2.setText(_translate("Dialog", "Go")) self.pushButton_3.setText(_translate("Dialog", "Open..")) self.label.setText(_translate("Dialog", "Sort TsMaster Config Byte Tool")) class RemoveTS(QWidget): def __init__(self, parent=None): super().__init__(parent) self.ui = Ui_Dialog() self.ui.setupUi(self) # self.ui.lineEdit.setText('select txt path') self.ui.label_2.setText('') self.ui.pushButton_3.clicked.connect(self.do_btn_open) self.ui.pushButton_2.clicked.connect(self.do_btn_go) def do_btn_open(self): open_path_full = QFileDialog.getOpenFileName(None, 'Select TsMaster txt file', '*.txt', 'All Files (*)') open_path = open_path_full[0] print('open_path is:', open_path) self.ui.lineEdit.setText(open_path) if open_path == '': self.ui.textEdit.setText('please select txt file first!') def do_btn_go(self): btn_go_not_empty=self.ui.lineEdit.text() print('1') if btn_go_not_empty =='': self.ui.textEdit.setText('please select txt file') else: txt_path = self.ui.lineEdit.text() config_list = [] str_result = '' with open(txt_path, 'r') as f: txt_data = f.readlines() txt_data_str = ''.join(txt_data) i = 0 for i in range(0, 10): config_num = txt_data_str.find('confi_' + str(i)) config_i_underline = txt_data_str[config_num + 9:config_num + 9 + 59] str_find = re.findall("[a-zA-Z0-9]", config_i_underline) str_config = ''.join(str_find) config_list.append(str_config) print(config_list) str_result = ''.join(config_list) print(str_result) str_result_len = len(str_result) self.ui.textEdit.setText(str_result) if str_result_len == 400: print('配置字长度标准是400字节,此次读取的长度是:', str_result_len) self.ui.label_2.setText(' PASS bytes length:400') self.ui.label_2.setStyleSheet('background-color:green') else: str_result_len_str = str(str_result_len) print('1') self.ui.label_2.setText(' FAIL bytes length: ' + str_result_len_str) self.ui.label_2.setStyleSheet('background-color:red') print('2') if __name__ == "__main__": app = QApplication(sys.argv) form = RemoveTS() form.show() sys.exit(app.exec_())
2.抓取安卓屏幕坐标
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# -*- coding: utf-8 -*- # @Time : 1/25/2022 # @Athor : WuHe # @File : FindDisplayPixMain.py #from pix import Ui_Form import os, time import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QWidget class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(659, 585) font = QtGui.QFont() font.setPointSize(10) Form.setFont(font) self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(320, 370, 121, 41)) font = QtGui.QFont() font.setPointSize(10) self.pushButton.setFont(font) self.pushButton.setStyleSheet("QPushButton{\n" "background-color:rgb(229, 241, 251);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:hover{\n" "background-color: rgb(170, 255, 255);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:pressed{\n" "background-color: rgb(207, 254, 245);\n" "border-style: inset;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "") self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(Form) self.pushButton_2.setGeometry(QtCore.QRect(490, 370, 121, 41)) self.pushButton_2.setStyleSheet("QPushButton{\n" "background-color:rgb(229, 241, 251);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:hover{\n" "background-color: rgb(170, 255, 255);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:pressed{\n" "background-color: rgb(207, 254, 245);\n" "border-style: inset;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "") self.pushButton_2.setObjectName("pushButton_2") self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(130, 30, 441, 31)) font = QtGui.QFont() font.setFamily("Mongolian Baiti") font.setPointSize(20) font.setBold(False) font.setItalic(False) font.setWeight(50) self.label.setFont(font) self.label.setStyleSheet("font: 20pt \"Mongolian Baiti\";") self.label.setObjectName("label") self.pushButton_3 = QtWidgets.QPushButton(Form) self.pushButton_3.setGeometry(QtCore.QRect(320, 128, 121, 41)) font = QtGui.QFont() font.setPointSize(10) self.pushButton_3.setFont(font) self.pushButton_3.setStyleSheet("QPushButton{\n" "background-color:rgb(229, 241, 251);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:hover{\n" "background-color: rgb(170, 255, 255);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:pressed{\n" "background-color: rgb(207, 254, 245);\n" "border-style: inset;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "") self.pushButton_3.setObjectName("pushButton_3") self.label_2 = QtWidgets.QLabel(Form) self.label_2.setGeometry(QtCore.QRect(480, 128, 41, 41)) self.label_2.setStyleSheet("") self.label_2.setText("") self.label_2.setObjectName("label_2") self.lineEdit = QtWidgets.QLineEdit(Form) self.lineEdit.setGeometry(QtCore.QRect(50, 128, 230, 30)) self.lineEdit.setStyleSheet("font: 63 10pt \"Yu Gothic UI Semibold\";\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "\n" "padding:5px;\n" "\n" "") self.lineEdit.setObjectName("lineEdit") self.label_3 = QtWidgets.QLabel(Form) self.label_3.setGeometry(QtCore.QRect(50, 100, 161, 16)) self.label_3.setStyleSheet("font: 10pt \"Yu Gothic UI Semilight\";") self.label_3.setObjectName("label_3") self.groupBox = QtWidgets.QGroupBox(Form) self.groupBox.setGeometry(QtCore.QRect(40, 160, 251, 411)) self.groupBox.setStyleSheet("font: 10pt \"Yu Gothic UI Semilight\";") self.groupBox.setObjectName("groupBox") self.textEdit = QtWidgets.QTextEdit(self.groupBox) self.textEdit.setGeometry(QtCore.QRect(10, 30, 231, 371)) self.textEdit.setStyleSheet("border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "padding:4px;\n" "\n" "") self.textEdit.setObjectName("textEdit") self.label_4 = QtWidgets.QLabel(Form) self.label_4.setGeometry(QtCore.QRect(320, 270, 281, 41)) self.label_4.setStyleSheet("\n" "") self.label_4.setText("") self.label_4.setObjectName("label_4") self.retranslateUi(Form) self.pushButton_2.clicked.connect(Form.close) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Get Coordinate")) self.pushButton.setText(_translate("Form", "Go..")) self.pushButton_2.setText(_translate("Form", "Exit")) self.label.setText(_translate("Form", "Get Android Coordinate Tool")) self.pushButton_3.setText(_translate("Form", "Connect..")) self.label_3.setText(_translate("Form", "Current Coordinate")) self.groupBox.setTitle(_translate("Form", "History")) class GetDisplayPixel(QWidget): def __init__(self, parent=None): super().__init__(parent) self.ui = Ui_Form() self.ui.setupUi(self) self.ui.textEdit.setText('') self.ui.lineEdit.setText('') self.ui.pushButton.clicked.connect(self.do_btn_go) self.ui.pushButton_3.clicked.connect(self.go_precondition) self.num = 0 self.connect_bool = 0 def do_btn_go(self): # 计算公式:(touch_size * wm_size / max) self.num += 1 if self.connect_bool == 1: self.ui.label_4.setText(' Ready to press display...: ' + str(self.num)) self.ui.label_4.setStyleSheet('background-color:green;'"font: 75 10pt 'MS Shell Dlg 2'") QApplication.processEvents() wm_x, wm_y = self.read_wm_size() get_x_max, get_y_max, dec_x, dec_y = self.get_max() # dec_x, dec_y ,get_device_name= read_getevent() x = int(dec_x * wm_x / get_x_max) y = int(dec_y * wm_y / get_y_max) # self.ui.lineEdit.setText(str(x) + ',' + str(y)) str_x = str(x) str_y = str(y) self.ui.lineEdit.setText(str_x + ',' + str_y) self.ui.textEdit.append('loop num is :' + str(self.num)) self.ui.textEdit.append(str_x + ',' + str_y) # QApplication.processEvents() time.sleep(0.2) else: self.ui.label_4.setText('Connect adb firstly!') QApplication.processEvents() def go_precondition(self): fun = os.popen('adb devices') fun_read = fun.readlines() devices_num = len(fun_read) if devices_num > 2: self.connect_bool = 1 else: self.connect_bool = 0 if self.connect_bool: self.ui.label_4.setText(' Connect Success! ') self.ui.label_4.setStyleSheet('background-color:green;'"font: 80 10pt 'MS Shell Dlg 2'") self.ui.label_2.setStyleSheet("border-radius:20px;" "background-color: rgb(0, 255, 0);" "border:2px groove gray;") else: self.ui.label_2.setStyleSheet("border-radius:20px;" "background-color: rgb(255, 0, 0);" "border:2px groove gray;") # 前提条件:电脑可以正常使用adb命令 def read_wm_size(self): fun = os.popen('adb shell wm size') fun_read = fun.readline() wm_xy = fun_read.split(':')[1].strip().split('x') wm_x = int(wm_xy[0]) wm_y = int(wm_xy[1]) print('屏幕物理尺寸的x:', wm_x) print('屏幕物理尺寸的y:', wm_y) return wm_x, wm_y def read_getevent(self): fun = os.popen('adb shell getevent -c 10') # fun = os.popen('adb shell getevent') print('请点击需要读取的屏幕位置') fun_read = [] # is list while fun_read == []: fun_read = fun.readlines() time.sleep(0.1) # print(fun_read) # to string str = ''.join(fun_read) # print(str) get_x_position = str.find('0035') get_x = str[get_x_position + 5:get_x_position + 5 + 8] get_y_position = str.find('0036') get_y = str[get_y_position + 5:get_y_position + 5 + 8] # to DEC dec_x = int(get_x.upper(), 16) dec_y = int(get_y.upper(), 16) print('点击的点的像素x:', dec_x) print('点击的点的像素y:', dec_y) # print('35 and 36:',get_x_position,get_y_position) # /dev/input/event2 get_device_35_36_str = str[get_x_position + 5 + 9:get_y_position - 7] # print('between 35-36:', get_device_35_36_str) get_device_index = str.find(get_device_35_36_str) get_device_name = str[get_device_index - 14:get_device_index - 2].strip() # print('device name:', get_device_name) return dec_x, dec_y, get_device_name def get_max(self): dec_x, dec_y, get_device_name = self.read_getevent() # print('1111:', get_device_name) # get_device_name='add device 2' fun = os.popen('adb shell getevent -p') fun_read = fun.readlines() str = ''.join(fun_read) get_mark = str.find(get_device_name) mark_str = str[get_mark:] # print(mark_str) get_x1_position = mark_str.find('0035') get_x2_position = mark_str.find('0036') get_x1 = mark_str[get_x1_position + 4:get_x2_position] get_x1_max = get_x1[get_x1.find('max'):] get_x1_max_1 = get_x1_max.find(',') # get_x1_max_1=8 get_x_max = int(get_x1_max[3:get_x1_max_1].strip()) + 1 print('屏幕像素点最大值x:', get_x_max) get_y1_position = mark_str.find('0036') get_y2_position = mark_str.find('0039') get_y1 = mark_str[get_y1_position + 4:get_y2_position] get_y1_max = get_y1[get_y1.find('max'):] get_y1_max_1 = get_y1_max.find(',') # get_x1_max_1=8 get_y_max = int(get_y1_max[3:get_y1_max_1].strip()) + 1 print('屏幕像素点最大值y:', get_y_max) return get_x_max, get_y_max, dec_x, dec_y if __name__ == "__main__": app = QApplication(sys.argv) form = GetDisplayPixel() form.show() sys.exit(app.exec_())
3.计数器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# -*- coding: utf-8 -*- # @Time : 2/11/2022 # @Athor : WuHe # @File : counterMain.py #from count import Ui_Form import time import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QWidget class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(581, 433) font = QtGui.QFont() font.setPointSize(10) Form.setFont(font) self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(280, 160, 121, 41)) font = QtGui.QFont() font.setPointSize(10) self.pushButton.setFont(font) self.pushButton.setStyleSheet("QPushButton{\n" "background-color:rgb(229, 241, 251);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:hover{\n" "background-color: rgb(170, 255, 255);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:pressed{\n" "background-color: rgb(207, 254, 245);\n" "border-style: inset;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "") self.pushButton.setObjectName("pushButton") self.pushButton_3 = QtWidgets.QPushButton(Form) self.pushButton_3.setGeometry(QtCore.QRect(280, 90, 121, 41)) font = QtGui.QFont() font.setPointSize(10) self.pushButton_3.setFont(font) self.pushButton_3.setStyleSheet("QPushButton{\n" "background-color:rgb(229, 241, 251);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:hover{\n" "background-color: rgb(170, 255, 255);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:pressed{\n" "background-color: rgb(207, 254, 245);\n" "border-style: inset;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "") self.pushButton_3.setObjectName("pushButton_3") self.label_2 = QtWidgets.QLabel(Form) self.label_2.setGeometry(QtCore.QRect(480, 108, 41, 41)) self.label_2.setStyleSheet("") self.label_2.setText("") self.label_2.setObjectName("label_2") self.lineEdit = QtWidgets.QLineEdit(Form) self.lineEdit.setGeometry(QtCore.QRect(50, 90, 180, 100)) self.lineEdit.setLayoutDirection(QtCore.Qt.LeftToRight) self.lineEdit.setStyleSheet("\n" "border-radius:10px; \n" "color: rgb(0, 162, 0);\n" "font: 30pt \"MS Shell Dlg 2\";\n" "border:2px groove gray;\n" "\n" "padding:5px;\n" "\n" "") self.lineEdit.setAlignment(QtCore.Qt.AlignCenter) self.lineEdit.setObjectName("lineEdit") self.label_3 = QtWidgets.QLabel(Form) self.label_3.setGeometry(QtCore.QRect(50, 80, 161, 16)) self.label_3.setStyleSheet("font: 10pt \"Yu Gothic UI Semilight\";") self.label_3.setText("") self.label_3.setObjectName("label_3") self.label_4 = QtWidgets.QLabel(Form) self.label_4.setGeometry(QtCore.QRect(240, 60, 281, 41)) self.label_4.setStyleSheet("") self.label_4.setText("") self.label_4.setObjectName("label_4") self.pushButton_4 = QtWidgets.QPushButton(Form) self.pushButton_4.setGeometry(QtCore.QRect(420, 90, 121, 41)) font = QtGui.QFont() font.setPointSize(10) self.pushButton_4.setFont(font) self.pushButton_4.setStyleSheet("QPushButton{\n" "background-color:rgb(229, 241, 251);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:hover{\n" "background-color: rgb(170, 255, 255);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:pressed{\n" "background-color: rgb(207, 254, 245);\n" "border-style: inset;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "") self.pushButton_4.setObjectName("pushButton_4") self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(220, 10, 141, 31)) font = QtGui.QFont() font.setFamily("Ravie") font.setPointSize(14) self.label.setFont(font) self.label.setObjectName("label") self.groupBox = QtWidgets.QGroupBox(Form) self.groupBox.setGeometry(QtCore.QRect(20, 50, 541, 181)) self.groupBox.setObjectName("groupBox") self.pushButton_2 = QtWidgets.QPushButton(self.groupBox) self.pushButton_2.setGeometry(QtCore.QRect(400, 110, 121, 41)) self.pushButton_2.setStyleSheet("QPushButton{\n" "background-color:rgb(229, 241, 251);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:hover{\n" "background-color: rgb(170, 255, 255);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:pressed{\n" "background-color: rgb(207, 254, 245);\n" "border-style: inset;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "") self.pushButton_2.setObjectName("pushButton_2") self.groupBox_2 = QtWidgets.QGroupBox(Form) self.groupBox_2.setGeometry(QtCore.QRect(20, 250, 541, 161)) self.groupBox_2.setObjectName("groupBox_2") self.textEdit = QtWidgets.QTextEdit(self.groupBox_2) self.textEdit.setGeometry(QtCore.QRect(30, 40, 195, 66)) self.textEdit.setStyleSheet("\n" "border-radius:10px; \n" "color: rgb(0, 162, 0);\n" "font: 23pt \"MS Shell Dlg 2\";\n" "border:2px groove gray;\n" "\n" "padding:2px;\n" "\n" "") self.textEdit.setObjectName("textEdit") self.pushButton_5 = QtWidgets.QPushButton(self.groupBox_2) self.pushButton_5.setGeometry(QtCore.QRect(260, 40, 121, 41)) font = QtGui.QFont() font.setPointSize(10) self.pushButton_5.setFont(font) self.pushButton_5.setStyleSheet("QPushButton{\n" "background-color:rgb(229, 241, 251);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:hover{\n" "background-color: rgb(170, 255, 255);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:pressed{\n" "background-color: rgb(207, 254, 245);\n" "border-style: inset;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "") self.pushButton_5.setObjectName("pushButton_5") self.pushButton_6 = QtWidgets.QPushButton(self.groupBox_2) self.pushButton_6.setGeometry(QtCore.QRect(260, 100, 121, 41)) font = QtGui.QFont() font.setPointSize(10) self.pushButton_6.setFont(font) self.pushButton_6.setStyleSheet("QPushButton{\n" "background-color:rgb(229, 241, 251);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:hover{\n" "background-color: rgb(170, 255, 255);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:pressed{\n" "background-color: rgb(207, 254, 245);\n" "border-style: inset;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "") self.pushButton_6.setObjectName("pushButton_6") self.pushButton_7 = QtWidgets.QPushButton(self.groupBox_2) self.pushButton_7.setGeometry(QtCore.QRect(400, 40, 121, 41)) font = QtGui.QFont() font.setPointSize(10) self.pushButton_7.setFont(font) self.pushButton_7.setStyleSheet("QPushButton{\n" "background-color:rgb(229, 241, 251);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:hover{\n" "background-color: rgb(170, 255, 255);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:pressed{\n" "background-color: rgb(207, 254, 245);\n" "border-style: inset;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "") self.pushButton_7.setObjectName("pushButton_7") self.groupBox.raise_() self.pushButton.raise_() self.pushButton_3.raise_() self.label_2.raise_() self.lineEdit.raise_() self.label_3.raise_() self.label_4.raise_() self.pushButton_4.raise_() self.label.raise_() self.groupBox_2.raise_() self.retranslateUi(Form) self.pushButton_2.clicked.connect(Form.close) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Counter")) self.pushButton.setText(_translate("Form", "-1")) self.pushButton_3.setText(_translate("Form", "+1")) self.pushButton_4.setText(_translate("Form", "Reset")) self.label.setText(_translate("Form", "Counter")) self.groupBox.setTitle(_translate("Form", "计数器")) self.pushButton_2.setText(_translate("Form", "Exit")) self.groupBox_2.setTitle(_translate("Form", "计时器")) self.pushButton_5.setText(_translate("Form", "Start")) self.pushButton_6.setText(_translate("Form", "Stop")) self.pushButton_7.setText(_translate("Form", "Reset")) class Counter(QWidget): def __init__(self, parent=None): super().__init__(parent) self.ui = Ui_Form() self.ui.setupUi(self) self.ui.lineEdit.setText('0') self.ui.pushButton_3.clicked.connect(self.do_btn_plus) self.ui.pushButton.clicked.connect(self.do_btn_subduction) self.ui.pushButton_4.clicked.connect(self.do_btn_reset) self.ui.pushButton_6.clicked.connect(self.do_btn_stop) self.ui.pushButton_5.clicked.connect(self.do_btn_start) self.ui.pushButton_7.clicked.connect(self.do_btn_reset2) self.ui.pushButton_2.clicked.connect(self.do_btn_exit) self.time_m = 1 self.time_s = 1 self.time_ms = 1 self.timer_true=True def do_btn_plus(self): read_value_str = self.ui.lineEdit.text() if read_value_str == '': read_value_str = 0 read_value = int(read_value_str) + 1 read_value_str_dis = str(read_value) self.ui.lineEdit.setText(read_value_str_dis) def do_btn_subduction(self): read_value_str = self.ui.lineEdit.text() if read_value_str == '': read_value_str = 0 read_value = int(read_value_str) - 1 read_value_str_dis = str(read_value) self.ui.lineEdit.setText(read_value_str_dis) def do_btn_reset(self): self.ui.lineEdit.setText('0') # time.struct_time(tm_year=2022, tm_mon=3, tm_mday=1, tm_hour=16, tm_min=6, tm_sec=47, tm_wday=1, tm_yday=60, tm_isdst=0) def do_btn_start(self): self.timer_true=True time_start = time.time() while self.timer_true: elapsed_time=time.time()-time_start self.time_m=int(elapsed_time/60) self.time_s=int(elapsed_time-self.time_m*60) time_ms_1=int((elapsed_time-self.time_s-self.time_m*60)*1000) self.time_ms=int(('%2d'% time_ms_1)) last_time=('%02d:%02d:%03d'% (self.time_m,self.time_s,self.time_ms)) self.ui.textEdit.setText(last_time) time.sleep(0.001) QApplication.processEvents() def do_btn_stop(self): self.timer_true=False def do_btn_reset2(self): self.timer_true = False time.sleep(0.01) reset_default = '00:00:000' self.ui.textEdit.setText(reset_default) def do_btn_exit(self): self.timer_true = False if __name__ == "__main__": app = QApplication(sys.argv) form = Counter() form.show() sys.exit(app.exec_())
4.beep闹钟
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import time import sys import winsound from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QWidget class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(737, 414) Form.setFixedSize(737,414) font = QtGui.QFont() font.setPointSize(10) Form.setFont(font) self.label_2 = QtWidgets.QLabel(Form) self.label_2.setGeometry(QtCore.QRect(480, 108, 41, 41)) self.label_2.setStyleSheet("") self.label_2.setText("") self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(Form) self.label_3.setGeometry(QtCore.QRect(50, 80, 161, 16)) self.label_3.setStyleSheet("font: 10pt \"Yu Gothic UI Semilight\";") self.label_3.setText("") self.label_3.setObjectName("label_3") self.label_4 = QtWidgets.QLabel(Form) self.label_4.setGeometry(QtCore.QRect(240, 60, 281, 41)) self.label_4.setStyleSheet("") self.label_4.setText("") self.label_4.setObjectName("label_4") self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(300, 10, 141, 31)) font = QtGui.QFont() font.setFamily("Ravie") font.setPointSize(14) self.label.setFont(font) self.label.setObjectName("label") self.groupBox = QtWidgets.QGroupBox(Form) self.groupBox.setGeometry(QtCore.QRect(20, 60, 691, 331)) self.groupBox.setObjectName("groupBox") self.pushButton_2 = QtWidgets.QPushButton(self.groupBox) self.pushButton_2.setGeometry(QtCore.QRect(570, 60, 81, 101)) self.pushButton_2.setStyleSheet("QPushButton{\n" "background-color:rgb(229, 241, 251);\n" "border-radius:10px; \n" "color: red;\n" "font: 12pt \"MS Shell Dlg 2\";\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:hover{\n" "background-color: rgb(170, 255, 255);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:pressed{\n" "background-color: rgb(207, 254, 245);\n" "border-style: inset;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "") self.pushButton_2.setObjectName("pushButton_2") self.lineEdit = QtWidgets.QLineEdit(self.groupBox) self.lineEdit.setGeometry(QtCore.QRect(110, 50, 221, 61)) self.lineEdit.setLayoutDirection(QtCore.Qt.LeftToRight) self.lineEdit.setStyleSheet("\n" "border-radius:10px; \n" "color: rgb(0, 162, 0);\n" "font: 15pt \"MS Shell Dlg 2\";\n" "border:2px groove gray;\n" "\n" "padding:5px;\n" "\n" "") self.lineEdit.setAlignment(QtCore.Qt.AlignCenter) self.lineEdit.setObjectName("lineEdit") self.label_5 = QtWidgets.QLabel(self.groupBox) self.label_5.setGeometry(QtCore.QRect(10, 60, 91, 40)) self.label_5.setObjectName("label_5") self.lineEdit_2 = QtWidgets.QLineEdit(self.groupBox) self.lineEdit_2.setGeometry(QtCore.QRect(110, 130, 221, 61)) self.lineEdit_2.setLayoutDirection(QtCore.Qt.LeftToRight) self.lineEdit_2.setStyleSheet("\n" "border-radius:10px; \n" "color: rgb(0, 162, 0);\n" "font: 15pt \"MS Shell Dlg 2\";\n" "border:2px groove gray;\n" "\n" "padding:5px;\n" "\n" "") self.lineEdit_2.setAlignment(QtCore.Qt.AlignCenter) self.lineEdit_2.setObjectName("lineEdit_2") self.label_7 = QtWidgets.QLabel(self.groupBox) self.label_7.setGeometry(QtCore.QRect(10, 140, 81, 40)) self.label_7.setObjectName("label_7") self.lineEdit_3 = QtWidgets.QLineEdit(self.groupBox) self.lineEdit_3.setGeometry(QtCore.QRect(110, 210, 221, 61)) self.lineEdit_3.setLayoutDirection(QtCore.Qt.LeftToRight) self.lineEdit_3.setStyleSheet("\n" "border-radius:10px; \n" "color: rgb(0, 162, 0);\n" "font: 15pt \"MS Shell Dlg 2\";\n" "border:2px groove gray;\n" "\n" "padding:5px;\n" "\n" "") self.lineEdit_3.setAlignment(QtCore.Qt.AlignCenter) self.lineEdit_3.setObjectName("lineEdit_3") self.label_8 = QtWidgets.QLabel(self.groupBox) self.label_8.setGeometry(QtCore.QRect(10, 220, 91, 40)) self.label_8.setObjectName("label_8") self.pushButton_4 = QtWidgets.QPushButton(self.groupBox) self.pushButton_4.setGeometry(QtCore.QRect(370, 60, 191, 101)) font = QtGui.QFont() font.setFamily("MS Shell Dlg 2") font.setPointSize(25) font.setBold(False) font.setItalic(False) font.setWeight(50) self.pushButton_4.setFont(font) self.pushButton_4.setStyleSheet("QPushButton{\n" "background-color:rgb(229, 241, 251);\n" "border-radius:10px; \n" "font: 25pt \"MS Shell Dlg 2\";\n" "color: Green;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:hover{\n" "background-color: rgb(170, 255, 255);\n" "border-radius:10px; \n" "color: black;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "\n" "QPushButton:pressed{\n" "background-color: rgb(207, 254, 245);\n" "border-style: inset;\n" "border:2px groove gray;\n" "border-radius: 10px;\n" "}\n" "") self.pushButton_4.setObjectName("pushButton_4") self.label_6 = QtWidgets.QLabel(self.groupBox) self.label_6.setGeometry(QtCore.QRect(370, 210, 291, 51)) self.label_6.setStyleSheet("color: rgb(255, 0, 0);\n" "font: 75 35pt \"Consolas\";") self.label_6.setObjectName("label_6") self.groupBox.raise_() self.label_2.raise_() self.label_3.raise_() self.label_4.raise_() self.label.raise_() self.retranslateUi(Form) self.pushButton_2.clicked.connect(Form.close) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Bee")) self.label.setText(_translate("Form", "Beep ...")) self.groupBox.setTitle(_translate("Form", "计时")) self.pushButton_2.setText(_translate("Form", "Exit")) self.lineEdit.setText(_translate("Form", "4")) self.label_5.setText(_translate("Form", "间隔时间M:")) self.lineEdit_2.setText(_translate("Form", "2")) self.label_7.setText(_translate("Form", "响铃时间S:")) self.lineEdit_3.setText(_translate("Form", "1000")) self.label_8.setText(_translate("Form", "响铃频率Hz:")) self.pushButton_4.setText(_translate("Form", "Start")) self.label_6.setText(_translate("Form", "00:00:000")) class Counter(QWidget): def __init__(self, parent=None): super().__init__(parent) self.ui = Ui_Form() self.ui.setupUi(self) self.ui.pushButton_4.clicked.connect(self.do_btn_start) self.ui.pushButton_2.clicked.connect(self.do_btn_exit) self.timer_true=False self.ui.label_6.setStyleSheet("color: rgb(134, 134, 134);\n""font: 75 35pt \"Consolas\";") #self.label_6.setStyleSheet("color: grey;\n""font: 75 35pt \"Consolas\";") # time.struct_time(tm_year=2022, tm_mon=3, tm_mday=1, tm_hour=16, tm_min=6, tm_sec=47, tm_wday=1, tm_yday=60, tm_isdst=0) def do_btn_start(self): cus_time=int(self.ui.lineEdit.text()) ring_time=int(self.ui.lineEdit_2.text()) ring_frequency=int(self.ui.lineEdit_3.text()) self.ui.label_6.setStyleSheet("color: rgb(255, 0, 0);\n""font: 75 35pt \"Consolas\";") if self.timer_true==False: #print('1') self.timer_true=True time_start = time.time() while self.timer_true: #print('2') elapsed_time = time.time() - time_start self.time_m = int(elapsed_time / 60) self.time_s = int(elapsed_time - self.time_m * 60) time_ms_1 = int((elapsed_time - self.time_s - self.time_m * 60) * 1000) self.time_ms = int(('%2d' % time_ms_1)) last_time = ('%02d:%02d:%03d' % (self.time_m, self.time_s, self.time_ms)) #print(last_time) #print(type(last_time)) self.ui.label_6.setText(last_time) time.sleep(0.05) QApplication.processEvents() if self.time_m==cus_time: winsound.Beep(ring_frequency, ring_time*1000) self.timer_true = False self.ui.label_6.setStyleSheet("color: rgb(0, 255, 0);\n""font: 75 35pt \"Consolas\";") def do_btn_exit(self): self.timer_true = False if __name__ == "__main__": app = QApplication(sys.argv) form = Counter() form.show() sys.exit(app.exec_())