Python3.7+PyQt5 pyuic5将.ui文件转换为.py文件、Python读取配置文件、生成日志

news/2024/12/22 14:40:45/

1.实际开发项目时,是使用Qt Designer来设计UI界面,得到一个.ui的文件,然后利用PyQt5安装时自带的工具pyuic5将.ui文件转换为.py文件:

pyuic5 -o mywindow.py mywindow.ui #先是py文件名,再是ui文件名

样式图 QT5 UI:

转换的pyqt5:

# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'ActionTools.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.
import socketfrom PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
import sys
import io
import os
import datetime
import random
from subprocess import Popen,PIPE
from threading import Thread
import time
import configparserclass Ui_ActionWinfrm(QWidget):def __init__(self):super(Ui_ActionWinfrm,self).__init__()self.config=configparser.ConfigParser()#创建对象self.EmployeeInfo=[]#员工信息self.TestServer=[]#测试服务器设置self.MesServer=[]#Mes服务器self.ProductInfo=[]#产品信息self.config.read("./conf/config.conf",encoding="utf-8")#读取配置文件,如果配置文件不存在创建self.Err="NULL"#员工管理信息self.EmployeeInfo.append(self.config.get('EmployeeInfo','Employee'))#工号self.EmployeeInfo.append(self.config.get('EmployeeInfo','Line'))#线体#测试服务器设置self.TestServer.append(self.config.get('TestServer','host'))self.TestServer.append(self.config.get('TestServer','prot'))self.TestServer.append(self.config.get('TestServer','user'))self.TestServer.append(self.config.get('TestServer','password'))#连接服务器self.host = self.TestServer[0]self.prot = self.TestServer[1]#MES服务器设置self.MesServer.append(self.config.get('MesServer','url'))#产品信息self.ProductInfo.append(self.config.get('ProductInfo','ClientName'))self.ProductInfo.append(self.config.get('ProductInfo','ProductName'))self.ProductInfo.append(self.config.get('ProductInfo','OrderInfo'))self.ProductInfo.append(self.config.get('ProductInfo','OrderTotal'))self.ProductInfo.append(self.config.get('ProductInfo','CompleteOrder'))self.data=''self.setupUi()print(self.host + "," + self.prot)self.Client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)def setupUi(self):self.setObjectName("ActionWinfrm")self.resize(1000, 943)font = QtGui.QFont()font.setFamily("Arial")font.setPointSize(12)self.setFont(font)icon = QtGui.QIcon()icon.addPixmap(QtGui.QPixmap("./Imag/020.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)self.setWindowIcon(icon)self.verticalLayout = QtWidgets.QVBoxLayout(self)self.verticalLayout.setObjectName("verticalLayout")self.frame_5 = QtWidgets.QFrame(self)self.frame_5.setFrameShape(QtWidgets.QFrame.StyledPanel)self.frame_5.setFrameShadow(QtWidgets.QFrame.Raised)self.frame_5.setObjectName("frame_5")self.frame_6 = QtWidgets.QFrame(self.frame_5)self.frame_6.setGeometry(QtCore.QRect(0, -1, 981, 41))self.frame_6.setStyleSheet("color: rgb(85, 255, 127);")self.frame_6.setFrameShape(QtWidgets.QFrame.StyledPanel)self.frame_6.setFrameShadow(QtWidgets.QFrame.Raised)self.frame_6.setObjectName("frame_6")self.lbl_WinTitle = QtWidgets.QLabel(self.frame_6)self.lbl_WinTitle.setGeometry(QtCore.QRect(390, 0, 351, 41))font = QtGui.QFont()font.setPointSize(26)self.lbl_WinTitle.setFont(font)self.lbl_WinTitle.setAlignment(QtCore.Qt.AlignCenter)self.lbl_WinTitle.setObjectName("lbl_WinTitle")self.lbl_loginfo = QtWidgets.QLabel(self.frame_6)self.lbl_loginfo.setGeometry(QtCore.QRect(10, 0, 161, 41))self.lbl_loginfo.setText("")self.lbl_loginfo.setPixmap(QtGui.QPixmap("./Imag/logo.jpg"))self.lbl_loginfo.setObjectName("lbl_loginfo")self.frame_7 = QtWidgets.QFrame(self.frame_5)self.frame_7.setGeometry(QtCore.QRect(0, 50, 981, 80))self.frame_7.setFrameShape(QtWidgets.QFrame.StyledPanel)self.frame_7.setFrameShadow(QtWidgets.QFrame.Raised)self.frame_7.setObjectName("frame_7")self.label = QtWidgets.QLabel(self.frame_7)self.label.setGeometry(QtCore.QRect(10, 10, 81, 16))self.label.setAlignment(QtCore.Qt.AlignCenter)self.label.setObjectName("label")self.label_3 = QtWidgets.QLabel(self.frame_7)self.label_3.setGeometry(QtCore.QRect(340, 10, 81, 16))self.label_3.setAlignment(QtCore.Qt.AlignCenter)self.label_3.setObjectName("label_3")self.label_4 = QtWidgets.QLabel(self.frame_7)self.label_4.setGeometry(QtCore.QRect(660, 10, 81, 16))self.label_4.setAlignment(QtCore.Qt.AlignCenter)self.label_4.setObjectName("label_4")self.label_2 = QtWidgets.QLabel(self.frame_7)self.label_2.setGeometry(QtCore.QRect(10, 50, 81, 16))self.label_2.setAlignment(QtCore.Qt.AlignCenter)self.label_2.setObjectName("label_2")self.label_5 = QtWidgets.QLabel(self.frame_7)self.label_5.setGeometry(QtCore.QRect(340, 50, 81, 16))self.label_5.setAlignment(QtCore.Qt.AlignCenter)self.label_5.setObjectName("label_5")self.label_6 = QtWidgets.QLabel(self.frame_7)self.label_6.setGeometry(QtCore.QRect(660, 50, 81, 16))self.label_6.setAlignment(QtCore.Qt.AlignCenter)self.label_6.setObjectName("label_6")self.label_7 = QtWidgets.QLabel(self.frame_7)self.label_7.setGeometry(QtCore.QRect(820, 50, 91, 20))self.label_7.setAlignment(QtCore.Qt.AlignCenter)self.label_7.setObjectName("label_7")self.lbl_ClientName = QtWidgets.QLabel(self.frame_7)self.lbl_ClientName.setGeometry(QtCore.QRect(100, 10, 151, 16))self.lbl_ClientName.setStyleSheet("color: rgb(255, 170, 0);")self.lbl_ClientName.setObjectName("lbl_ClientName")self.lbl_ProductName = QtWidgets.QLabel(self.frame_7)self.lbl_ProductName.setGeometry(QtCore.QRect(420, 10, 54, 12))self.lbl_ProductName.setStyleSheet("color: rgb(255, 170, 0);")self.lbl_ProductName.setObjectName("lbl_ProductName")self.lbl_OrderInfo = QtWidgets.QLabel(self.frame_7)self.lbl_OrderInfo.setGeometry(QtCore.QRect(730, 10, 101, 16))self.lbl_OrderInfo.setAutoFillBackground(False)self.lbl_OrderInfo.setStyleSheet("color: rgb(255, 170, 0);")self.lbl_OrderInfo.setObjectName("lbl_OrderInfo")self.lbl_Employee = QtWidgets.QLabel(self.frame_7)self.lbl_Employee.setGeometry(QtCore.QRect(90, 50, 91, 16))self.lbl_Employee.setStyleSheet("color: rgb(255, 170, 0);")self.lbl_Employee.setObjectName("lbl_Employee")self.lbl_Line = QtWidgets.QLabel(self.frame_7)self.lbl_Line.setGeometry(QtCore.QRect(420, 50, 81, 16))self.lbl_Line.setStyleSheet("color: rgb(255, 170, 0);")self.lbl_Line.setObjectName("lbl_Line")self.lbl_OrderTotal = QtWidgets.QLabel(self.frame_7)self.lbl_OrderTotal.setGeometry(QtCore.QRect(730, 50, 54, 12))self.lbl_OrderTotal.setStyleSheet("color: rgb(255, 170, 0);")self.lbl_OrderTotal.setObjectName("lbl_OrderTotal")self.lbl_COrderNum = QtWidgets.QLabel(self.frame_7)self.lbl_COrderNum.setGeometry(QtCore.QRect(910, 50, 54, 19))self.lbl_COrderNum.setStyleSheet("color: rgb(255, 170, 0);")self.lbl_COrderNum.setObjectName("lbl_COrderNum")self.textEdit = QtWidgets.QTextEdit(self.frame_5)self.textEdit.setGeometry(QtCore.QRect(0, 40, 981, 1))self.textEdit.setObjectName("textEdit")self.TW_TestItemList = QtWidgets.QTableWidget(self.frame_5)self.TW_TestItemList.setGeometry(QtCore.QRect(0, 180, 981, 631))font = QtGui.QFont()font.setPointSize(12)self.TW_TestItemList.setFont(font)self.TW_TestItemList.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)self.TW_TestItemList.setShowGrid(False)self.TW_TestItemList.setObjectName("TW_TestItemList")self.TW_TestItemList.setColumnCount(3)self.TW_TestItemList.setRowCount(0)self.item = QtWidgets.QTableWidgetItem()self.item.setTextAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignTop)font = QtGui.QFont()font.setFamily("Arial")font.setPointSize(12)self.item.setFont(font)icon1 = QtGui.QIcon()icon1.addPixmap(QtGui.QPixmap("Imag/NewAdd.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)self.item.setIcon(icon1)self.TW_TestItemList.setHorizontalHeaderItem(0, self.item)self.item = QtWidgets.QTableWidgetItem()self.item.setTextAlignment(QtCore.Qt.AlignCenter)font = QtGui.QFont()font.setFamily("Arial")font.setPointSize(12)self.item.setFont(font)icon2 = QtGui.QIcon()icon2.addPixmap(QtGui.QPixmap("Imag/TestArgs.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)self.item.setIcon(icon2)self.TW_TestItemList.setHorizontalHeaderItem(1, self.item)self.item = QtWidgets.QTableWidgetItem()self.item.setTextAlignment(QtCore.Qt.AlignCenter)font = QtGui.QFont()font.setFamily("Arial")font.setPointSize(12)self.item.setFont(font)icon3 = QtGui.QIcon()icon3.addPixmap(QtGui.QPixmap("Imag/Result.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)self.item.setIcon(icon3)self.TW_TestItemList.setHorizontalHeaderItem(2, self.item)self.TW_TestItemList.horizontalHeader().setMinimumSectionSize(40)self.TW_TestItemList.verticalHeader().setDefaultSectionSize(50)self.TW_TestItemList.verticalHeader().setMinimumSectionSize(50)self.label_8 = QtWidgets.QLabel(self.frame_5)self.label_8.setGeometry(QtCore.QRect(10, 140, 112, 25))self.label_8.setObjectName("label_8")self.lbl_TestResult = QtWidgets.QLabel(self.frame_5)self.lbl_TestResult.setGeometry(QtCore.QRect(0, 815, 981, 111))self.lbl_TestResult.setStyleSheet("background-color: rgb(0, 0, 0);\n""font: 22pt \"Arial\";\n""color: rgb(255, 255, 0);")self.lbl_TestResult.setAlignment(QtCore.Qt.AlignCenter)self.lbl_TestResult.setObjectName("lbl_TestResult")self.lbl_Isn = QtWidgets.QLineEdit(self.frame_5)self.lbl_Isn.setGeometry(QtCore.QRect(130, 140, 800, 31))self.lbl_Isn.setObjectName("lbl_Isn")self.verticalLayout.addWidget(self.frame_5)self.retranslateUi()QtCore.QMetaObject.connectSlotsByName(self)self.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint | QtCore.Qt.WindowCloseButtonHint)  # 只显示最小化按钮和关闭按钮self.lbl_Isn.setFocus()def retranslateUi(self):self._translate = QtCore.QCoreApplication.translateself.setWindowTitle(self._translate("ActionWinfrm", "【T.e.s.t】"))self.lbl_WinTitle.setText(self._translate("ActionWinfrm", "国产系统在线激活工具"))self.label.setText(self._translate("ActionWinfrm", "客户名称:"))self.label_3.setText(self._translate("ActionWinfrm", "产品名称:"))self.label_4.setText(self._translate("ActionWinfrm", "订单号:"))self.label_2.setText(self._translate("ActionWinfrm", "员工工号:"))self.label_5.setText(self._translate("ActionWinfrm", "线          体:"))self.label_6.setText(self._translate("ActionWinfrm", "订单数:"))self.label_7.setText(self._translate("ActionWinfrm", "完成订单数:"))self.lbl_ClientName.setText(self.ProductInfo[0])#客户名称self.lbl_ProductName.setText(self.ProductInfo[1])#产品名称self.lbl_OrderInfo.setText(self.ProductInfo[2])#订单信息self.lbl_OrderTotal.setText(self.ProductInfo[3])#订单总数self.lbl_COrderNum.setText(self.ProductInfo[4])#完成订单数self.lbl_Employee.setText(self.EmployeeInfo[0])#工号self.lbl_Line.setText(self.EmployeeInfo[1])#线体self.item = self.TW_TestItemList.horizontalHeaderItem(0)self.item.setText(self._translate("ActionWinfrm", "项目名称"))self.item = self.TW_TestItemList.horizontalHeaderItem(1)self.item.setText(self._translate("ActionWinfrm", "测试参数"))self.item = self.TW_TestItemList.horizontalHeaderItem(2)self.item.setText(self._translate("ActionWinfrm", "测试结果"))self.TW_TestItemList.setColumnWidth(0,300)self.TW_TestItemList.setColumnWidth(1, 370)self.TW_TestItemList.setColumnWidth(2, 300)self.TW_TestItemList.setShowGrid(False)#是否显示网线self.label_8.setText(self._translate("ActionWinfrm", "扫描整机条码:"))self.lbl_TestResult.setText(self._translate("ActionWinfrm", "待测试"))def Center(self):#定义一个函数使得函数窗口居中显示#获取屏幕坐标系self.screen=QDesktopWidget().screenGeometry()#获取窗口坐标系self.size=self.geometry()self.newLeft=(self.screen.width()-self.size.width())/2self.newTop=(self.screen.height()-self.size.height())/2.7self.move(int(self.newLeft),int(self.newTop))def Connect(self,message):try:self.Client_socket.connect(self.host,int(self.port))self.lbl_TestResult.setText("Connected To Server..")self.lbl_TestResult.setForeground(QtGui.QColor(51, 204, 51))self.Client_socket.sendall(message.encode())self.data=self.Client_socket.recv(1024)except Exception as e:self.lbl_TestResult.setText("Connected To Server Err:"+str(e))self.lbl_TestResult.setStyleSheet("color:red")return Falseself.Client_socket.close()return Trueif __name__ == '__main__':app = QApplication(sys.argv)win = Ui_ActionWinfrm()win.Center()win.show()#win.Client('10.2.230.10', 8888)win.Connect('tell me the product name and position:00E04C060621')sys.exit(app.exec_())

效果:

2.Python学习之读取配置文件:

初始配置文件test.conf:

步骤1:导入配置文件模块

import configparser

步骤2:创建配置文件对象

config = configparser.ConfigParser()

步骤3:读取配置文件
在这一步中,我们将读取配置文件。配置文件通常存储在.ini或.conf文件中。

config.read('config.ini')

步骤4:获取配置选项

option_value = config.get('section_name', 'option_name')

步骤5:修改配置选项

config.set('section_name', 'option_name', 'new_value')

步骤6:保存配置文件

with open('config.ini', 'w') as config_file:config.write(config_file)

*.展正读取confg的实列:

3.生成日志:

1.创建日志记录器

import logging# 创建一个日志记录器实例
logger = logging.getLogger('my_logger')

2 设置日志记录级别

logger.setLevel(logging.DEBUG)使用setLevel()方法设置日志记录的级别,可以根据需要进行调整。常用的日志级别有:logging.DEBUG:最详细的日志信息,用于调试程序。
logging.INFO:一般的日志信息,用于确认程序正常运行。
logging.WARNING:警告信息,表明程序可能出现问题。
logging.ERROR:错误信息,程序出现错误但仍然可以继续运行。
logging.CRITICAL:严重错误信息,程序无法继续运行。

3.创建文件处理器

file_handler = logging.FileHandler('log.txt')

4.设置文件处理器的格式

formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)

5.将处理器添加到记录器中

logger.addHandler(file_handler)

6.输出日志信息

logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')


http://www.ppmy.cn/news/1230113.html

相关文章

安全项目简介

安全项目 基线检查 密码 复杂度有效期 用户访问和身份验证 禁用administrator禁用guest认证失败锁定 安全防护软件操作系统安全配置 关闭自动播放 文件和目录权限端口限制安全审计… 等保测评 是否举办了安全意识培训是否有应急响应预案有无第一负责人 工作内容 测评准备…

数据结构【DS】图的基本概念

定义 完全图(简单完全图) 完全无向图:边数为𝐧𝐧−𝟏𝟐完全有向图:边数为 𝐧(𝐧−𝟏) 子图、生成子图 G的子图:所有的顶点和边都属于图G的图 G的生成子图…

CISP全真模式测试题(二)

免责声明 文章仅做经验分享用途,利用本文章所提供的信息而造成的任何直接或者间接的后果及损失,均由使用者本人负责,作者不为此承担任何责任,一旦造成后果请自行承担!!! 1、下列关于信息安全保障的说法错误的是: A.信息安全保障的问题就是安全的效用问题,在解决或预…

腾讯云轻量数据库是什么?轻量数据库测评详细介绍

腾讯云轻量数据库测评,轻量数据库100%兼容MySQL 5.7和8.0,腾讯云提供1C1G20GB、1C1G40GB、1C2G80GB、2C4G120GB、2C8G240GB五种规格轻量数据库,腾讯云百科txybk.com分享腾讯云轻量数据库测评、轻量数据库详细介绍、特性、配置价格和常见问题解…

​vmware虚拟机ubuntu系统配置静态ip​

把虚拟机当成服务器,如果虚拟机的ip是一直变化的,每次远程连接需要都修改连接虚拟机的ip地址,这肯定是麻烦的。 一、设置一下本机的VMnet8的ip 配置路径:控制面板->所有控制面板项->网络和共享中心 二、首先设置NAT 选自…

22年+21年 计算机能力挑战赛初赛C语言程序题 题解

22年 第14题&#xff1a;答案&#xff1a;33 #include<stdio.h> int x1; int f(int a) { static int x2;int n0;if(a%2){ static int x3;nx; }else { static int x5;nx; }return nx;} void main() { int sumx,i;for(i0;i<4;i) sumf(i); printf(&qu…

vs添加 高级保存选项

工具-自定义-命令-菜单栏-文件-添加命令-文件-高级保存选项

IDEA调用接口超时,但Postman可成功调用接口

&#x1f4e2;专注于分享软件测试干货内容&#xff0c;欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; 如有错误敬请指正&#xff01;&#x1f4e2;交流讨论&#xff1a;欢迎加入我们一起学习&#xff01;&#x1f4e2;资源分享&#xff1a;耗时200小时精选的「软件测试」资…