问题描述: 选择自己图片目录,随机更换壁纸
效果演示
问题解决:
直接上代码:
import sys
import os
import platform
import random
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox
import win32api, win32gui, win32con
class Ui_changePaperWindow(object):
def setupUi(self, changePaperWindow):
changePaperWindow.setObjectName("changePaperWindow")
changePaperWindow.resize(400, 300)
changePaperWindow.setIconSize(QtCore.QSize(24, 24))
self.centralwidget = QtWidgets.QWidget(changePaperWindow)
self.centralwidget.setObjectName("centralwidget")
self.showFoldText = QtWidgets.QLineEdit(self.centralwidget)
self.showFoldText.setGeometry(QtCore.QRect(140, 50, 200, 50))
self.showFoldText.setObjectName("showFoldText")
self.selectFoldBtn = QtWidgets.QPushButton(self.centralwidget)
self.selectFoldBtn.setGeometry(QtCore.QRect(40, 50, 70, 50))
self.selectFoldBtn.setObjectName("selectFoldBtn")
self.changeBtn = QtWidgets.QPushButton(self.centralwidget)
self.changeBtn.setGeometry(QtCore.QRect(140, 140, 140, 80))
self.changeBtn.setObjectName("changeBtn")
changePaperWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(changePaperWindow)
QtCore.QMetaObject.connectSlotsByName(changePaperWindow)
def retranslateUi(self, changePaperWindow):
_translate = QtCore.QCoreApplication.translate
changePaperWindow.setWindowTitle(_translate("changePaperWindow", "随机更换壁纸"))
self.showFoldText.setText(_translate("changePaperWindow", " 选择您需要随机更换壁纸的目录"))
self.selectFoldBtn.setText(_translate("changePaperWindow", "选择目录"))
self.changeBtn.setText(_translate("changePaperWindow", "马上随机更换壁纸"))
self.selectFoldBtn.clicked.connect(self.selectFold)
self.changeBtn.clicked.connect(self.changAction)
imgArr = []
imgFold = ''
def selectFold(self):
fold = QtWidgets.QFileDialog.getExistingDirectory(None, "选取带图片的文件夹", "C:/")
self.imgFold = fold
self.showFoldText.setText(fold)
if (os.path.exists(fold)):
list = os.listdir(fold)
for i in range(0, len(list)):
filePath = os.path.join(fold, list[i])
if (os.path.isfile(filePath) and ( filePath.lower().find('jpg') > 1 or filePath.lower().find('png') > 1 ) ):
self.imgArr.append(filePath)
else:
print(filePath + "不是文件")
else:
self.showFoldText.setText("您选择的目录不存在")
def changAction(self):
if (self.imgFold == ""):
return QMessageBox.information(None, "请选择路径", "还没有选择路径", QMessageBox.Yes)
elif(len(self.imgArr) < 1):
return QMessageBox.information(None, "没有图片文件", "该文件夹下面没有图片文件", QMessageBox.Yes)
system_version = platform.platform()
system = system_version.split('-')[0]
if system == 'Windows' or system == 'windows':
randNmb = random.randint(0, len(self.imgArr))
else:
if os.path.exists('/temp/bg'):
print('the temp folder exists')
else:
os.mkdir('temp/bg')
imgRoot = self.imgArr[randNmb]
self.changeWallpaper(imgRoot)
def changeWallpaper(self, pic):
system_version = platform.platform()
print(system_version)
system = system_version.split('-')[0]
try:
if system == 'Windows' or system == 'windows':
regKey = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(regKey, "WallpaperStyle", 0, win32con.REG_SZ, "2")
win32api.RegSetValueEx(regKey, "TileWallpaper", 0, win32con.REG_SZ, "0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, pic, win32con.SPIF_SENDWININICHANGE)
else:
os.system('gsettings set org.gnome.desktop.background picture-uri "file:/tmp/img/bg.img"')
os.system('gsettings set org.gnome.desktop.background picture-options "centered"')
except:
print('something wrong with detect system')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QMainWindow()
window = Ui_changePaperWindow()
window.setupUi(widget)
widget.show()
sys.exit(app.exec())
|