上一篇文章用python制作了一个自动清理内存的程序,利用cmd端口调用Rammap,不过只是做了一个托盘图标,这回用pyqt5做一个简单的图形界面,并实现对自动清理模式的一些设置
首先利用pyqt5工具qt designer生成UI界面,这里命名为F_UI.ui(并利用信号/槽编辑器将滑动条和微调框绑定,实现数值同步)
然后利用PyUIC把Qt Designer生成的.ui文件换成.py文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'F-ram.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(220, 10, 571, 561))
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(10, 40, 201, 161))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Sunken)
self.frame.setObjectName("frame")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(60, 0, 81, 20))
self.label.setObjectName("label")
self.line = QtWidgets.QFrame(self.frame)
self.line.setGeometry(QtCore.QRect(10, 120, 181, 16))
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.pushButton = QtWidgets.QPushButton(self.frame)
self.pushButton.setGeometry(QtCore.QRect(10, 20, 181, 21))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.frame)
self.pushButton_2.setGeometry(QtCore.QRect(10, 40, 181, 21))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.frame)
self.pushButton_3.setGeometry(QtCore.QRect(10, 60, 181, 21))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.frame)
self.pushButton_4.setGeometry(QtCore.QRect(10, 80, 181, 21))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.frame)
self.pushButton_5.setGeometry(QtCore.QRect(10, 100, 181, 21))
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_6 = QtWidgets.QPushButton(self.frame)
self.pushButton_6.setGeometry(QtCore.QRect(10, 130, 181, 21))
self.pushButton_6.setObjectName("pushButton_6")
self.frame_2 = QtWidgets.QFrame(self.centralwidget)
self.frame_2.setGeometry(QtCore.QRect(10, 210, 201, 141))
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Sunken)
self.frame_2.setObjectName("frame_2")
self.label_2 = QtWidgets.QLabel(self.frame_2)
self.label_2.setGeometry(QtCore.QRect(70, 0, 61, 20))
self.label_2.setObjectName("label_2")
self.checkBox = QtWidgets.QCheckBox(self.frame_2)
self.checkBox.setGeometry(QtCore.QRect(10, 50, 31, 16))
self.checkBox.setChecked(True)
self.checkBox.setObjectName("checkBox")
self.checkBox_3 = QtWidgets.QCheckBox(self.frame_2)
self.checkBox_3.setGeometry(QtCore.QRect(50, 50, 31, 16))
self.checkBox_3.setChecked(True)
self.checkBox_3.setObjectName("checkBox_3")
self.checkBox_2 = QtWidgets.QCheckBox(self.frame_2)
self.checkBox_2.setGeometry(QtCore.QRect(90, 50, 31, 16))
self.checkBox_2.setChecked(True)
self.checkBox_2.setObjectName("checkBox_2")
self.checkBox_4 = QtWidgets.QCheckBox(self.frame_2)
self.checkBox_4.setGeometry(QtCore.QRect(130, 50, 31, 16))
self.checkBox_4.setChecked(True)
self.checkBox_4.setObjectName("checkBox_4")
self.checkBox_5 = QtWidgets.QCheckBox(self.frame_2)
self.checkBox_5.setGeometry(QtCore.QRect(170, 50, 31, 16))
self.checkBox_5.setChecked(True)
self.checkBox_5.setObjectName("checkBox_5")
self.horizontalSlider = QtWidgets.QSlider(self.frame_2)
self.horizontalSlider.setGeometry(QtCore.QRect(60, 90, 131, 22))
self.horizontalSlider.setMinimum(30)
self.horizontalSlider.setMaximum(300)
self.horizontalSlider.setProperty("value", 60)
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setTickPosition(QtWidgets.QSlider.NoTicks)
self.horizontalSlider.setObjectName("horizontalSlider")
self.label_3 = QtWidgets.QLabel(self.frame_2)
self.label_3.setGeometry(QtCore.QRect(80, 70, 41, 20))
self.label_3.setObjectName("label_3")
self.spinBox = QtWidgets.QSpinBox(self.frame_2)
self.spinBox.setGeometry(QtCore.QRect(11, 90, 41, 22))
self.spinBox.setMinimum(30)
self.spinBox.setMaximum(300)
self.spinBox.setProperty("value", 60)
self.spinBox.setObjectName("spinBox")
self.pushButton_7 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_7.setGeometry(QtCore.QRect(10, 20, 181, 21))
self.pushButton_7.setObjectName("pushButton_7")
self.pushButton_8 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_8.setGeometry(QtCore.QRect(110, 112, 75, 21))
self.pushButton_8.setObjectName("pushButton_8")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
self.horizontalSlider.valueChanged['int'].connect(self.spinBox.setValue)
self.spinBox.valueChanged['int'].connect(self.horizontalSlider.setValue)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
self.label.setText(_translate("MainWindow", "Empty options"))
self.pushButton.setText(_translate("MainWindow", "Empty working sets"))
self.pushButton_2.setText(_translate("MainWindow", "Empty system working sets"))
self.pushButton_3.setText(_translate("MainWindow", "Empty modified page list"))
self.pushButton_4.setText(_translate("MainWindow", "Empty standby list"))
self.pushButton_5.setText(_translate("MainWindow", "Empty priority 0 standby list"))
self.pushButton_6.setText(_translate("MainWindow", "Empty all"))
self.label_2.setText(_translate("MainWindow", "Automatic"))
self.checkBox.setText(_translate("MainWindow", "Ew"))
self.checkBox_3.setText(_translate("MainWindow", "Es"))
self.checkBox_2.setText(_translate("MainWindow", "Em"))
self.checkBox_4.setText(_translate("MainWindow", "Et"))
self.checkBox_5.setText(_translate("MainWindow", "E0"))
self.label_3.setText(_translate("MainWindow", "Time/s"))
self.pushButton_7.setText(_translate("MainWindow", "Empty automatic"))
self.pushButton_8.setText(_translate("MainWindow", "Save"))
新建MainWindow.py用于启动图形界面并保存关于自动清理的设置(生成_Ea_.py)
import F_UI # 导入QtTest文件
from sys import exit, argv
from subprocess import Popen
from PyQt5.QtWidgets import QMainWindow, QApplication
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(QMainWindow, self).__init__(parent)
self.ui = F_UI.Ui_MainWindow()
self.ui.setupUi(self)
# 定义按钮
self.button_w = self.ui.pushButton
self.button_s = self.ui.pushButton_2
self.button_m = self.ui.pushButton_3
self.button_t = self.ui.pushButton_4
self.button_0 = self.ui.pushButton_5
self.button_l = self.ui.pushButton_6
self.button_a = self.ui.pushButton_7
self.button_save = self.ui.pushButton_8
# 按钮信号绑定
self.button_w.clicked.connect(self.click_button)
self.button_s.clicked.connect(self.click_button)
self.button_m.clicked.connect(self.click_button)
self.button_t.clicked.connect(self.click_button)
self.button_0.clicked.connect(self.click_button)
self.button_l.clicked.connect(self.click_button)
self.button_a.clicked.connect(self.click_button)
self.button_save.clicked.connect(self.click_button)
self.button_a.setCheckable(True)
def click_button(self):
sender = self.sender()
print(sender.text() + '被点击')
if sender.text() == 'Save':
x = self.ui.horizontalSlider.value()
with open("_Ea_.py", "w") as f:
f.write("from subprocess import Popen\n\n\n")
f.write("class RunEa:\n")
f.write("\tdef __init__(self):\n")
f.write("\t\tself.sleep_time = "+str(x)+"\n\n")
f.write("\tdef rPopen(self):\n")
if self.ui.checkBox.isChecked():
f.write("\t\tPopen('RAMMap -Ew', shell=True).wait()\n")
if self.ui.checkBox_2.isChecked():
f.write("\t\tPopen('RAMMap -Es', shell=True).wait()\n")
if self.ui.checkBox_3.isChecked():
f.write("\t\tPopen('RAMMap -Em', shell=True).wait()\n")
if self.ui.checkBox_4.isChecked():
f.write("\t\tPopen('RAMMap -Et', shell=True).wait()\n")
if self.ui.checkBox_5.isChecked():
f.write("\t\tPopen('RAMMap -E0', shell=True).wait()\n")
elif sender.text() == 'Empty automatic':
if self.button_a.isChecked():
Popen("start R_Ea.exe", shell=True)
else:
Popen("taskkill /F /IM R_Ea.exe", shell=True)
elif sender.text() == 'Empty all':
Popen("RAMMap -Ew", shell=True).wait()
Popen("RAMMap -Es", shell=True).wait()
Popen("RAMMap -Em", shell=True).wait()
Popen("RAMMap -Et", shell=True).wait()
Popen("RAMMap -E0", shell=True).wait()
elif sender.text() == 'Empty working sets':
Popen("RAMMap -Ew", shell=True)
elif sender.text() == 'Empty system working sets':
Popen("RAMMap -Es", shell=True)
elif sender.text() == 'Empty modified page list':
Popen("RAMMap -Em", shell=True)
elif sender.text() == 'Empty standby list':
Popen("RAMMap -Et", shell=True)
elif sender.text() == 'Empty priority 0 standby list':
Popen("RAMMap -E0", shell=True)
if __name__ == '__main__':
app = QApplication(argv)
window = MainWindow()
window.show()
exit(app.exec_())
'''
from PyQt5 import QtCore, QtGui, QtWidgets
if __name__ == '__main__':
# 获取UIC窗口操作权限
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
# 调自定义的界面(即刚转换的.py对象)
Ui = F_UI.Ui_MainWindow() # 这里也引用了一次F_connect.py文件的名字注意
Ui.setupUi(MainWindow)
# 显示窗口并释放资源
MainWindow.show()
sys.exit(app.exec_())
'''
'''
setCheckable() 设置按钮是否已经被选中,如果设置为True,则表示按钮将保持已点击和释放状态
toggle() 在按钮状态之间进行转换
setIcon() 设置按钮上的图标
setEnabled() 设置按钮是否可以使用,当设置为False时,按钮变成不可用状态,点击它不会发射信号
isChecked() 返回按钮的状态,返回值为True或False
setDefault() 设置按钮的默认状态
setText() 设置按钮的显示文本
text() 返回按钮的显示文本
'''
新建R_Ea.py用于读取保存的设置并实现自动清理
from time import sleep
import _Ea_
E = _Ea_.RunEa()
while True:
E.rPopen()
sleep(E.sleep_time)
生成的_Ea_.py
from subprocess import Popen
class RunEa:
def __init__(self):
self.sleep_time = 60
def rPopen(self):
Popen('RAMMap -Ew', shell=True).wait()
Popen('RAMMap -Es', shell=True).wait()
Popen('RAMMap -Em', shell=True).wait()
Popen('RAMMap -Et', shell=True).wait()
Popen('RAMMap -E0', shell=True).wait()
主文件 F-Ram.py
from ctypes import windll
from sys import executable
from wx import adv, App
# 用于创建托盘图标
from PIL import Image
from pystray import MenuItem, Icon
# 用于调用cmd
from subprocess import Popen
automatic = False
empty_path = 6
'''
def RAMMap_run():
cmd = 'RAMMap-64bit -Ew'
res = Popen(cmd)
output_str = res.read()
print(output_str)
'''
# 启动和关闭自动释放内存的文件
def Automatic():
global automatic, i
if automatic == True:
automatic = False
Popen('taskkill /F /IM R_Ea.exe',shell=True) #强行终止进程
#托盘气泡提示
adv.NotificationMessage("RAMMap-64bit", message="已结束释放内存\n Automatic turn off").Show()
elif automatic == False:
automatic = True
Popen("start R_Ea.exe",shell=True)#启动(也可以用.py)
# adv.NotificationMessage("").SetIcon(wx.Icon("ASNO.png")).Show()
#托盘气泡提示
adv.NotificationMessage("RAMMap-64bit", message="正在自动释放内存\n Automatic have already turn on").Show()
def Empty(path):
# cmd调用RAMMap外部接口
if path == 0:
# 这里一定要阻塞运行,否则RAMMAP会报错
Popen("RAMMap -Ew",shell=True).wait()
Popen("RAMMap -Es",shell=True).wait()
Popen("RAMMap -Em",shell=True).wait()
Popen("RAMMap -Et",shell=True).wait()
Popen("RAMMap -E0",shell=True).wait()
elif path == 1:
Popen("RAMMap -Ew",shell=True)
elif path == 2:
Popen("RAMMap -Es",shell=True)
elif path == 3:
Popen("RAMMap -Em",shell=True)
elif path == 4:
Popen("RAMMap -Et",shell=True)
elif path == 5:
Popen("RAMMap -E0",shell=True)
# 判断点击的选项
def click_menu(icon, item):
global empty_path
print("点击了", item)
if str(item) == 'Empty automatic':
Automatic()
elif str(item) == 'Empty all':
empty_path = 0
elif str(item) == 'Empty Ew':
empty_path = 1
elif str(item) == 'Empty Es':
empty_path = 2
elif str(item) == 'Empty Em':
empty_path = 3
elif str(item) == 'Empty Et':
empty_path = 4
elif str(item) == 'Empty E0':
empty_path = 5
Empty(empty_path)
empty_path = 6
# 关闭程序
def on_exit(icon, item):
icon.stop()
def on_open():
Popen("start MainWindow.exe", shell=True)
def is_admin():
try:
return windll.shell32.IsUserAnAdmin()
except:
return False
app = App(0)
# flag = adv.NotificationMessage.SetFlags("ASNO.png")
# 以管理员运行
if is_admin():
# 创建托盘
menu = (MenuItem(text='Empty all', action=click_menu),
MenuItem(text='Empty Ew', action=click_menu),
MenuItem(text='Empty Es', action=click_menu),
MenuItem(text='Empty Em', action=click_menu),
MenuItem(text='Empty Et', action=click_menu),
MenuItem(text='Empty E0', action=click_menu),
MenuItem(text='Empty automatic', action=click_menu),
MenuItem(text='Open', action=on_open),
MenuItem(text='Exit', action=on_exit),
)
image = Image.open("ASNO.png")# 托盘显示的图标
icon = Icon("name", image, "RAMMap-64bit\nRUN", menu)# 鼠标放在托盘上显示的文字
icon.run()
else:
windll.shell32.ShellExecuteW(None, "runas", executable, __file__, None, 1)
效果
程序和源码下载
F-Ram基于RAMMap的自动内存释放工具(含源码)-Python文档类资源-CSDN文库
F-Ram基于RAMMap的自动内存释放工具(含源码)python-Python文档类资源-CSDN文库文章来源:https://www.toymoban.com/news/detail-506895.html
文章来源地址https://www.toymoban.com/news/detail-506895.html
到了这里,关于RAMMAP(运行内存清理工具)自动释放内存,并利用pyqt5制作图形界面的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!