当前位置: 首页 > article >正文

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

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 socket

from 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 configparser

class 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.translate
        self.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())/2
        self.newTop=(self.screen.height()-self.size.height())/2.7
        self.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 False
        self.Client_socket.close()
        return True

if __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.kler.cn/a/133223.html

相关文章:

  • Azure pipeline 通过git命令修改文件
  • Spring Boot3 实战案例合集上线了
  • 使用React和Vite构建一个AirBnb Experiences克隆网站
  • 《C语言程序设计现代方法》note-4 基本类型 强制类型转换 类型定义
  • Ubuntu 18 EDK2 环境编译
  • 初识Linux · 共享内存
  • uni-app小程序开发使用uView,u-model传入富文本内容过长,真机上无法滚动
  • 【2023年csp-j第二轮】第一题解析
  • 【算法挨揍日记】day29——139. 单词拆分、467. 环绕字符串中唯一的子字符串
  • 【设计一个缓存--针对各种类型的缓存】
  • 【数据分享】2023年我国省市县三级的专精特新“小巨人”企业数量(Excel/Shp格式)
  • 【LeetCode刷题-滑动窗口】-- 239.滑动窗口最大值
  • 【【萌新的SOC学习之 VDMA 彩条显示实验之一】】
  • 【RocketMq系列-01】RocketMq安装和基本概念
  • TG Pro v2.87(mac温度风扇速度控制工具)
  • 拒绝服务攻击工具的编写
  • 永久关机windows系统自动更新
  • linux时间调整
  • 使用记录-MongoDB
  • openai/chatgpt的api接口,各个模型的最大输入token一览表
  • 适用于全部安卓手机的 5 大免费 Android 数据恢复
  • 037、目标检测-算法速览
  • 「git 系列」git 如何存储代码的?
  • 6.9平衡二叉树(LC110-E)
  • WPF实现将鼠标悬浮在按钮上时弹出菜单
  • MyBatis逆向工程