首页 > 基础资料 博客日记

PYQT5 简单项目实践

2024-10-20 11:00:10基础资料围观81

这篇文章介绍了PYQT5 简单项目实践,分享给大家做个参考,收藏Java资料网收获更多编程知识

在VSCode编辑器我们通过引入pyqt5,用QTdesigner 实现拖拽实现图形化界面

下面我们实现一个简单项目实践一下吧

效果图:

用法:Python编写逻辑,用pyqt实现界面显示。

功能:

  1. 第一行把处理的数据文件拖拽到文本框中
  2. 第二行是对文件数据中相同项的哪一行取值的处理,比如取相同产品的第一条数据我们可以输入数值1
  3. 选择框B\R则是表示从前取值/从后取值

实现流程:

下面我们就直接讲述怎么拖拽实现图像化界

桌面先创建一个文件夹,vscode中打开该文件夹,右键(参考前一篇文章)启用pyqt的qtdesigner进行图形化界面设计,首先创建一个widget窗体。

再窗体上对组件进行拖拽设计,首先布局水平方向的设计,再水平方向上的基础上拖入lable,linetext,radiobutton,pushBotton组件,raddiobutton之间加入了横向的horizontal spacer 熟称弹簧使组件间具有一定的间隙。

设计完成后保存,如下图就会看到.UI的文件,pyuic5对其进行编译(前一篇文章有提)生成ui

-xxx.py文件

代码部分

整体代码结构(fristwin.py为启动文件;getdata.ui图形化设计,ui_getdata.py图形化设计的转译,以及getdataBR.py四个文件构成)

ui_getdataBR.py代码参考 

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file '/Users/a1234/Desktop/pyqt5/Getdata.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# 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.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(515, 352)
        self.verticalLayoutWidget = QtWidgets.QWidget(Form)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 10, 481, 311))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.verticalLayout.addItem(spacerItem)
        self.lineEdit = QtWidgets.QLineEdit(self.verticalLayoutWidget)
        self.lineEdit.setObjectName("lineEdit")
        self.verticalLayout.addWidget(self.lineEdit)
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.verticalLayout.addItem(spacerItem1)
        self.lineEdit_2 = QtWidgets.QLineEdit(self.verticalLayoutWidget)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.verticalLayout.addWidget(self.lineEdit_2)
        self.radioButton = QtWidgets.QRadioButton(self.verticalLayoutWidget)
        self.radioButton.setObjectName("radioButton")
        self.verticalLayout.addWidget(self.radioButton)
        spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.verticalLayout.addItem(spacerItem2)
        self.radioButton_2 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
        self.radioButton_2.setObjectName("radioButton_2")
        self.verticalLayout.addWidget(self.radioButton_2)
        self.pushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton)
        spacerItem3 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.verticalLayout.addItem(spacerItem3)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "该工具用于对数据进行处理取值(B是从前取值,R从后取值)"))
        self.lineEdit.setPlaceholderText(_translate("Form", "拖拽数据文件"))
        self.lineEdit_2.setPlaceholderText(_translate("Form", "输入数值获取几条数据"))
        self.radioButton.setText(_translate("Form", "B"))
        self.radioButton.setChecked(True)
        self.radioButton_2.setText(_translate("Form", "R"))
        self.pushButton.setText(_translate("Form", "提交"))

 getdataBR.py代码参考,定义一个类,对数据文件进行逻辑处理

import os
import pandas as pd
import numpy as np
import csv
class GetDataBR:
    def getvalue(data,reverse,countdata):
        data_temp=pd.read_csv(data,header=1,low_memory=False)
        with open(data,'r') as f:
            reader=csv.reader(f)
            header_row=next(reader)
            df =pd.read_csv(f,keep_default_na=False,low_memory=False)
            df_row = pd.DataFrame([header_row],columns=df.columns)
            df_fake = pd.DataFrame([df.columns],columns= df.columns)
            df_head = df.head(5)
        if reverse=="B":
            information=data_temp.groupby(['SerialNumber']).apply(lambda x: x.iloc[:countdata])
        elif reverse=="R":
            information=data_temp.groupby(['SerialNumber']).apply(lambda x:x.iloc[-countdata:])
        else:
            pass
        #print(information)
        datafinal = pd.concat([df_row,df_fake,df_head,information])
        filename = os.path.join(data.split(".")[0]+'append.csv')
        datafinal.to_csv(filename,header=None,index=False) 
        del data_temp
        return datafinal

 firstWin.py代码参考,把业务逻辑代码和界面设计进行联合实现一个简单的数据处理工具

# -*- coding: utf-8 -*-


import sys

from PyQt5.QtGui import QDropEvent
from GetDataBR import *
import os
from Ui_Getdata import Ui_Form
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow,QPushButton,QMessageBox,QFileDialog
from PyQt5 import QtCore

class MyMianWindow(QMainWindow,Ui_Form):
    def __init__(self, parent=None):
        super(QMainWindow,self).__init__(parent)
        self.setupUi(self)
        self.setWindowTitle('数据处理工具')
        self.setObjectName('win')
        self.setStyleSheet('#win{background-color:white;}')
        self.initButton()

    def initButton(self):
        self.lineEdit.installEventFilter(QEventHandler(self))
        self.pushButton.clicked.connect(self.process_msg)

    #定义一个函数实现数据处理功能
    def process_msg(self):
        load_csv =self.lineEdit.text()
        countdata=self.lineEdit_2.text()
        if self.radioButton.isChecked()==True:
            reverse=self.radioButton.text()
        elif self.radioButton_2.isChecked()==True:
            reverse=self.radioButton_2.text()
        else:
            pass
        print(load_csv,countdata,reverse)
        self.imessage=GetDataBR.getvalue(load_csv,reverse,int(countdata))
        if self.imessage.empty==True:
            QMessageBox.information(self, '提示', 'do not complete the task')
        else:
            QMessageBox.information(self, '提示', 'complete the task')

#定义事件处理机制文件的拖拽功能
class QEventHandler(QtCore.QObject):
    def eventFilter(self,obj,evnet):
        if evnet.type()==QtCore.QEvent.DragEnter:
            evnet.accept()
        if evnet.type()==QtCore.QEvent.Drop:
            md=evnet.mimeData()
            if md.hasUrls():
                url=md.urls()[0]
                obj.setText(url.toLocalFile())
                return True
        return super().eventFilter(obj,evnet)
    
if __name__=="__main__":
    app = QApplication(sys.argv)  
    ui=MyMianWindow() 
    ui.show()
    sys.exit(app.exec_())

到此就实现了开头所示的功能。

fighting~~~~~~


文章来源:https://blog.csdn.net/qq_43541919/article/details/143079314
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云