问题
家里买了个联想个人云盘,但是使用SAMBA映射到windows本地磁盘时,每次只能连接到一个账户上,于是想着写个小工具,方便切换多个用户账户,自动映射到本地磁盘
开发简介
开发语言:python
主要实现功能:
- 通过配置文件配置要连接的账户信息
- 选择不同账户时能够自动连接上并映射到本地磁盘
相关界面
主界面:
配置文件:
主要源码
net_store_switch.py
import os
import sys
# 导入QT,其中包含一些常量,例如颜色等
from functools import partial
from PyQt5 import QtCore, QtWidgets
# 使用调色板等
from PyQt5.QtGui import QIcon
# 导入常用组件
from PyQt5.QtWidgets import QApplication, QMainWindow
import NetStoreSwitchWindow
import tools.system_tool as system_tool
from tools import file_tool
# pyinstaller -F -i favicon.ico -w net_store_switch.py -n 联想个人云盘连接助手
class MainWindow(QMainWindow, NetStoreSwitchWindow.Ui_Dialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
start_top = -10
for index, user_info in enumerate(user_list):
start_top += 50
user = user_info['user']
password = user_info['password']
name = user_info['name']
globals()[f'pushButton{index}'] = QtWidgets.QPushButton(self)
globals()[f'pushButton{index}'].setGeometry(QtCore.QRect(20, start_top, 261, 41))
globals()[f'pushButton{index}'].setObjectName(f'pushButton{index}')
globals()[f'pushButton{index}'].setText(f'映射[{name}]的云盘到[{disk_flag}]盘')
globals()[f'pushButton{index}'].clicked.connect(partial(self.button_clicked, user_info))
start_top += 10
self.setMinimumSize(QtCore.QSize(303, 46 + start_top))
self.setMaximumSize(QtCore.QSize(303, 46 + start_top))
self.setWindowIcon(QIcon('favicon.ico'))
self.setWindowTitle(title)
self.statusLabel.setText(self.get_status())
def button_clicked(self, user_info):
sender = self.sender()
print(sender.text())
print(user_info)
user = user_info['user']
password = user_info['password']
name = user_info['name']
system_tool.command('net use * /delete /y')
system_tool.command(rf'net use {disk_flag} \\{ip_address}\{user} /user:{user} ' + password + " /persistent:yes")
os.system('start explorer')
self.statusLabel.setText(f'已连接到[{name}]个人云盘')
def get_status(self):
status = "当前未连接到个人云盘"
result = system_tool.command('net use')
if system_tool.is_ok(result):
data = result["data"]
for user_info in user_list:
user = user_info['user']
name = user_info['name']
if data.find(user) >= 0:
status = f"已连接到[{name}]个人云盘"
break
return status
if __name__ == '__main__':
config_file_path = file_tool.root_dir('configs.json')
configs = file_tool.read_configs(config_file_path)
print(configs)
title = configs['title']
ip_address = configs['ip_address']
disk_flag = configs['disk_flag']
user_list = configs['user_list']
app = QApplication(sys.argv)
# 创建一个主窗口
mainWin = MainWindow()
# 显示
mainWin.show()
# 主循环
sys.exit(app.exec_())
NetStoreSwitchWindow.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(303, 246)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(Dialog.sizePolicy().hasHeightForWidth())
Dialog.setSizePolicy(sizePolicy)
Dialog.setMinimumSize(QtCore.QSize(303, 246))
Dialog.setMaximumSize(QtCore.QSize(303, 246))
self.statusLabel = QtWidgets.QLabel(Dialog)
self.statusLabel.setGeometry(QtCore.QRect(30, 10, 241, 16))
self.statusLabel.setObjectName("statusLabel")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "联想个人云盘助手"))
self.statusLabel.setText(_translate("Dialog", "当前未连接到联想个人云盘"))
工具下载
https://download.csdn.net/download/guangdeshishe/89436365
解压后参考样例配置configs.json文件即可