# -*- coding:utf-8 -*-
# @Time:2020-07-25 10:24
# @Author:冯亮
# @Software:PyCharm
from PyQt5.QtWidgets import QWidget,QApplication,QToolBox,QPushButton,QLabel,QGridLayout,QGroupBox,QToolButton,\
QLineEdit,QComboBox,QRadioButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt,QSize
import sys,webbrowser
from openpyxl import load_workbook
import os,shutil,sip
def get_link_path(url):
from win32com.client import Dispatch
shell = Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(url)
return shortcut.Targetpath
class Dropedit(QLineEdit): # subclass
'''自定义单行文本,实现文件拖放获取路径'''
def __init__(self, parent=None):
super(Dropedit, self).__init__(parent)
self.setDragEnabled(True)
self.setAcceptDrops(True)
self.setFixedHeight(25)
def dragEnterEvent(self, event):
print("dragEnterEvent:")
if event.mimeData().hasUrls():
event.accept() # must accept the dragEnterEvent or else the dropEvent can't occur !!!
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls(): # if file or link is dropped
url = event.mimeData().urls()[0] # get first url
path_link=url.toString().replace('file:///','')
path_name_type= path_link.split('/')[-1]
path_name=path_name_type.split('.')[0]
path_type=path_name_type.split('.')[-1]
if path_type == 'lnk':
self.setText(get_link_path(path_link))
elif path_type=='exe':
self.setText(path_link)
class ToolWindow(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
# 窗口设置
self.setWindowIcon(QIcon('./image/1.png'))
self.setWindowTitle('我的软件--分类管理工具')
self.setFixedSize(300,600)
# 添加区域
self.groupbox1=QGroupBox()
self.grid_box=QGridLayout(self.groupbox1)
self.add_btn=QPushButton('增加')
self.add_btn.clicked.connect(self.add_btn_click)
self.update_btn = QPushButton('刷新工具')
self.update_btn.clicked.connect(self.update_btn_click)
# 添加隐藏控件
self.hide_kj()
self.grid_box.addWidget(self.add_btn,0,0,1,1)
self.grid_box.addWidget(self.update_btn, 0, 2, 1, 1)
# 设置抽屉及按钮
self.setToolBox()
# 定义样式
grid=QGridLayout()
self.setLayout(grid)
# 加入控件
grid.addWidget(self.groupbox1)
grid.addWidget(self.toolbox)
def hide_kj(self):
# 隐藏的控件
self.radio_btn1 = QRadioButton('软件')
self.radio_btn1.clicked.connect(self.radio_btn_click)
self.radio_btn2 = QRadioButton('网站')
self.radio_btn2.clicked.connect(self.radio_btn_click)
self.ok_btn = QPushButton('OK')
self.ok_btn.clicked.connect(self.ok_btn_click)
self.lab4 = QLabel('软件分类:')
self.lab1 = QLabel('软件名称:')
self.lab2 = QLabel('图标地址:')
self.lab3 = QLabel('软件路径:')
self.add_soft_name = QLineEdit()
self.add_soft_ico = QLineEdit()
self.add_soft_url = Dropedit()
self.comboBox = QComboBox()
self.comboBox.setObjectName("comboBox")
def ok_btn_click(self):
if self.radio_btn1.isChecked():
sheetName=self.comboBox.currentText()
url=self.add_soft_url.text()
print(url)
path_name_type = url.split('\\')[-1]
path_name = path_name_type.split('.')[0]
self.add_soft_name.setText(path_name)
ico_path="./images/%s/%s.ico"%(sheetName,path_name)
print(ico_path)
if not os.path.exists('./images/%s'%sheetName):
os.makedirs('./images/%s'%sheetName)
if not os.path.exists(ico_path):
print('no')
self.get_exe_ico(url)
# 写入拖放的APP信息
workbook = load_workbook("./软件列表.xlsx")
print(workbook.sheetnames)
sheet = workbook[sheetName]
if path_name !='' and path_name != '' and url !='':
sheet.append([path_name,path_name+'.ico', url])
workbook.save("./软件列表.xlsx")
workbook.close()
else:
return
if self.radio_btn2.isChecked():
sheetName=self.comboBox.currentText()
url=self.add_soft_url.text()
print(url)
webName=self.add_soft_name.text()
ico_old_path=self.add_soft_ico.text()
ico_old_path_last = ico_old_path.split('/')[-1]
ico_old_path_type = ico_old_path_last.split('.')[-1]
ico_new_path='./images/%s/'%sheetName+webName+'.'+ico_old_path_type
if not os.path.exists('./images/%s'%sheetName):
os.makedirs('./images/%s'%sheetName)
if not os.path.exists(ico_new_path):
print('no')
shutil.copy(ico_old_path,ico_new_path)
# 写入拖放的APP信息
workbook = load_workbook("./软件列表.xlsx")
print(workbook.sheetnames)
sheet = workbook[sheetName]
if webName != '' and ico_old_path_type != '' and url != '':
sheet.append([webName,webName+'.'+ico_old_path_type, url])
workbook.save("./软件列表.xlsx")
workbook.close()
else:
return
def radio_btn_click(self):
# 添加软件
if self.radio_btn1.isChecked():
# 展示控件
self.grid_box.addWidget(self.lab3, 2, 0, 1, 1)
self.grid_box.addWidget(self.add_soft_url, 2, 1, 1, 2)
self.grid_box.addWidget(self.lab1, 3, 0, 1, 1)
self.grid_box.addWidget(self.add_soft_name, 3, 1, 1, 2)
self.grid_box.addWidget(self.lab4, 4, 0, 1, 1)
for i in self.getSheetNames():
self.comboBox.addItem(i)
self.grid_box.addWidget(self.comboBox, 4, 1, 1, 2)
self.add_btn.setEnabled(False)
if self.radio_btn2.isChecked():
# 网站
self.lab3.setText('网址URL:')
self.grid_box.addWidget(self.lab3, 2, 0, 1, 1)
self.grid_box.addWidget(self.add_soft_url, 2, 1, 1, 2)
self.lab1.setText('网站名称:')
self.grid_box.addWidget(self.lab1, 3, 0, 1, 1)
self.grid_box.addWidget(self.add_soft_name, 3, 1, 1, 2)
self.grid_box.addWidget(self.lab2,4, 0, 1, 1)
self.grid_box.addWidget(self.add_soft_ico, 4, 1, 1, 2)
self.grid_box.addWidget(self.lab4, 5, 0, 1, 1)
for i in self.getSheetNames():
self.comboBox.addItem(i)
self.grid_box.addWidget(self.comboBox, 5, 1, 1, 2)
self.add_btn.setEnabled(False)
def getSheetNames(self):
workbook = load_workbook("./软件列表.xlsx")
return workbook.sheetnames
def add_btn_click(self):
self.hide_kj()
self.grid_box.addWidget(self.radio_btn1, 1, 0, 1, 1)
self.grid_box.addWidget(self.radio_btn2, 1, 1, 1, 1)
self.grid_box.addWidget(self.ok_btn, 1, 2, 1, 1)
self.add_btn.setEnabled(False)
self.update_btn.setEnabled(True)
def update_btn_click(self):
delete_list=[self.lab1,self.radio_btn1,self.radio_btn2,self.ok_btn,self.lab2,self.lab3,
self.lab4,self.add_soft_name,self.add_soft_ico,self.add_soft_url,self.comboBox]
for i in delete_list:
self
评论0