Pyqt5模拟Linux按上下键查历史命令

Pyqt5模拟Linux按上下键查历史命令

  本次使用pyqt5实现类似Linux按上下键查询历史命令。核心是通过一个list列表来存放历史命令,每次按上下键在list列表中查找历史命令,填充到命令行编辑框内。

说明

  代码使用说明:(按方向键之前需要让文本框失去焦点,只需要点击一下空白部分即可)先按左键,然后再按上或者下键查找命令,最后在按右键确定。

示例

Pyqt5模拟Linux按上下键查历史命令_第1张图片

源码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : cuntou0906
# @File    : TestPy.py
# @Time    : 2021/7/8 18:26
import sys
from PyQt5.QtWidgets import QApplication, QWidget,QListView,QFrame
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QAction, QMessageBox,QLineEdit


from PyQt5 import QtCore
from PyQt5.QtCore import *

CmdHistoryList = []                    # 历史命令列表
CmdHistoryList_currentIndex = 0        # 历史命令索引
class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(0, 0, 300, 200)
        self.setFixedWidth(300)
        self.setFixedHeight(200)
        self.setWindowTitle('模拟Shell历史命令')

        self.listView_12 = QListView(self)
        self.listView_12.setGeometry(QtCore.QRect(0, 0, 300, 200))
        self.listView_12.setStyleSheet("border-color: rgb(144, 144, 144);")
        self.listView_12.setFrameShape(QFrame.StyledPanel)
        self.listView_12.setFrameShadow(QFrame.Plain)
        self.listView_12.setLineWidth(2)
        self.listView_12.setObjectName("listView_12")

        self.CmdlineEdit = QLineEdit(self)
        self.CmdlineEdit.move(20, 20)
        self.CmdlineEdit.resize(200, 20)

        self.SendCmd = QPushButton('Send Cmd', self)
        self.SendCmd.move(20, 40)
        self.SendCmd.clicked.connect(self.on_click)
        self.show()

    def on_click(self):
        cmdStr = self.CmdlineEdit.text()
        if cmdStr=='':
            return
        # print(cmdStr)
        global CmdHistoryList_currentIndex,CmdHistoryList
        try:
            if len(CmdHistoryList) > 30:         # 保存历史命令
                CmdHistoryList.pop(0)
                CmdHistoryList.append(cmdStr)
            else:
                CmdHistoryList.append(cmdStr)
            print(CmdHistoryList)
        except:
            pass
        pass

    def keyPressEvent(self, event):
        global CmdHistoryList_currentIndex,CmdHistoryList

        if (event.key() == Qt.Key_Left):
            CmdHistoryList_currentIndex = len(CmdHistoryList)-1
            self.SendCmd.setEnabled(False)
            # print('测试:Key_Left')
        elif (event.key() == Qt.Key_Right):
            CmdHistoryList_currentIndex = len(CmdHistoryList)-1
            self.SendCmd.setEnabled(True)
            # print('测试:Key_Right')
        elif (event.key() == Qt.Key_Up):
            if len(CmdHistoryList)==0 or self.SendCmd.isEnabled():
                return
            # print('测试:Up')
            CmdHistoryList_currentIndex = CmdHistoryList_currentIndex - 1
            if CmdHistoryList_currentIndex <= 0:
                CmdHistoryList_currentIndex = 0
            self.CmdlineEdit.setText(CmdHistoryList[CmdHistoryList_currentIndex])
        elif (event.key() == Qt.Key_Down):
            if len(CmdHistoryList)==0 or self.SendCmd.isEnabled():
                return
            CmdHistoryList_currentIndex = CmdHistoryList_currentIndex + 1
            if CmdHistoryList_currentIndex>=len(CmdHistoryList)-1:
                CmdHistoryList_currentIndex=len(CmdHistoryList)-1
            # print('测试:Down')
            self.CmdlineEdit.setText(CmdHistoryList[CmdHistoryList_currentIndex])
        else:
            pass
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())

你可能感兴趣的:(#,▶,Python脚本,python,pyqt5,命令行模拟,linux)