上编blog说到用Python开发Windows桌面应用程序的环境搭建,下面就说说如何实现一个简单的远程日志读取的功能。
打开界面绘制工具,选中项目后,右键找到External Tools,然后选择QtDesigner
打开Qt Designer工作台,然后我们选择Main Window就可以在这个画布上尽情的绘制我们需要的界面呈现样式了
我绘制好的界面如下(远程查询Linux服务器上的日志)
我这里做的功能非常简单,连接服务器,然后查询设备对应的原始数据,这里有两个按钮,我们首先给按钮绑定点击事件
然后弹出按钮配置界面
因为我们绑定的是点击事件,所以这里选择第一个clicked()
点击最后一步的+号,创建一个点击事件,然后点击OK即可
将创建好的界面保存后,我们回到pycharm可以看到.ui文件
最后将.ui文件通过PyUIC转成对应的.py文件
最后我们看一下.py文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainForm.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# 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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1434, 790)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.B_Query = QtWidgets.QPushButton(self.centralwidget)
self.B_Query.setGeometry(QtCore.QRect(60, 480, 141, 41))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.B_Query.setFont(font)
self.B_Query.setObjectName("B_Query")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(20, 323, 81, 31))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.dTE_Start = QtWidgets.QDateTimeEdit(self.centralwidget)
self.dTE_Start.setGeometry(QtCore.QRect(100, 420, 201, 31))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.dTE_Start.setFont(font)
self.dTE_Start.setDate(QtCore.QDate(2020, 1, 1))
self.dTE_Start.setObjectName("dTE_Start")
self.E_ID = QtWidgets.QLineEdit(self.centralwidget)
self.E_ID.setGeometry(QtCore.QRect(100, 323, 200, 30))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.E_ID.setFont(font)
self.E_ID.setObjectName("E_ID")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(20, 420, 81, 31))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.E_IP = QtWidgets.QLineEdit(self.centralwidget)
self.E_IP.setGeometry(QtCore.QRect(100, 10, 200, 30))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.E_IP.setFont(font)
self.E_IP.setObjectName("E_IP")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 10, 78, 26))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.E_Psd = QtWidgets.QLineEdit(self.centralwidget)
self.E_Psd.setGeometry(QtCore.QRect(100, 90, 200, 30))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.E_Psd.setFont(font)
self.E_Psd.setObjectName("E_Psd")
self.B_Connect = QtWidgets.QPushButton(self.centralwidget)
self.B_Connect.setGeometry(QtCore.QRect(60, 130, 141, 41))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.B_Connect.setFont(font)
self.B_Connect.setObjectName("B_Connect")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(20, 90, 43, 26))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.E_User = QtWidgets.QLineEdit(self.centralwidget)
self.E_User.setGeometry(QtCore.QRect(100, 50, 200, 30))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.E_User.setFont(font)
self.E_User.setObjectName("E_User")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(10, 55, 60, 26))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(310, 0, 1121, 781))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.textEdit = QtWidgets.QTextEdit(self.verticalLayoutWidget)
self.textEdit.setLayoutDirection(QtCore.Qt.LeftToRight)
self.textEdit.setLineWidth(0)
self.textEdit.setObjectName("textEdit")
self.verticalLayout_3.addWidget(self.textEdit)
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(100, 370, 201, 31))
self.comboBox.setObjectName("comboBox")
self.label_7 = QtWidgets.QLabel(self.centralwidget)
self.label_7.setGeometry(QtCore.QRect(20, 370, 81, 31))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_7.setFont(font)
self.label_7.setObjectName("label_7")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
self.B_Connect.clicked.connect(MainWindow.connectServer_click)
self.B_Query.clicked.connect(MainWindow.queryRowData_click)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.B_Query.setText(_translate("MainWindow", "查询"))
self.label_4.setText(_translate("MainWindow", "设备ID:"))
self.dTE_Start.setDisplayFormat(_translate("MainWindow", "yyyy-MM-dd"))
self.label_5.setText(_translate("MainWindow", "选择日期:"))
self.label.setText(_translate("MainWindow", "服务器IP:"))
self.B_Connect.setText(_translate("MainWindow", "连接"))
self.label_3.setText(_translate("MainWindow", "密码:"))
self.label_2.setText(_translate("MainWindow", "用户名:"))
self.label_7.setText(_translate("MainWindow", "协议类型:"))
主方法里面实现代码如下:
#coding=gbk
#coding=utf-8
#-*- coding: UTF-8 -*
import tkinter
import tkinter.messagebox
import paramiko
from PyQt5.QtCore import QDate
from MainForm import Ui_MainWindow
from PyQt5 import QtWidgets
import sys
import re
class query_window(QtWidgets.QMainWindow):
client=None
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.dTE_Start.setDate(QDate.currentDate())
self.ui.comboBox.addItem('JT701')
self.ui.comboBox.addItem('JT701-TRA')
self.ui.comboBox.addItem('JT709A/JT705A/JT707A')
#B_Connect 的 点击动作绑定一个事件处理函数
def connectServer_click(self):
global client
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(self.ui.E_IP.text(), 22, self.ui.E_User.text(), self.ui.E_Psd.text(), compress=True)
root = tkinter.Tk()
root.withdraw()
if client != None:
tkinter.messagebox.showinfo("提示", "连接成功")
else:
tkinter.messagebox.showinfo("提示", "连接失败")
# B_Query 的 点击动作绑定一个事件处理函数
def queryRowData_click(self):
assetType = self.ui.comboBox.currentText()
global logPath
if assetType=='JT701':
logPath="/opt/gnss/jt701-server/logs/jt701-server-"+self.ui.dTE_Start.date().toString("yyyy-MM-dd")+".log"
elif assetType=='JT701-TRA':
logPath = "/opt/gnss/tra701-server/logs/tra701-server-" + self.ui.dTE_Start.date().toString("yyyy-MM-dd")+ ".log"
else:
logPath = "/opt/gnss/jt707-server/logs/jt707-server-" + self.ui.dTE_Start.date().toString("yyyy-MM-dd")+ ".log"
remote_file = client.open_sftp().open(logPath)
try:
for line in remote_file:
if line.find("ProtocolDecoder") != -1 & line.find(self.ui.E_ID.text())!=-1:
result = re.findall("INFO(.*)收到:", line)
for rinfo in result:
rline = line.replace(rinfo, '').replace('INFO', '')
self.ui.textEdit.append(rline)
finally:
remote_file.close()
self.ui.textEdit.append("END--------------------------------------------------------------------------")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = query_window()
window.show()
sys.exit(app.exec_())
最后运行后界面截图: