导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
统计
 

一、Python创桌面建快捷方式的2个例子

例子一:

 

代码如下:

import os
import pythoncom
from win32com.shell import shell    
from win32com.shell import shellcon

 

def createDesktopLnk(filename,lnkname):
    shortcut = pythoncom.CoCreateInstance(    
        shell.CLSID_ShellLink, None,    
        pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)    
    shortcut.SetPath(filename)    
    if os.path.splitext(lnkname)[-1] != '.lnk':    
        lnkname += ".lnk"
    # get desktop path
    desktopPath = shell.SHGetPathFromIDList(shell.SHGetSpecialFolderLocation(0,shellcon.CSIDL_DESKTOP))
    lnkname = os.path.join(desktopPath,lnkname)
    shortcut.QueryInterface(pythoncom.IID_IPersistFile).Save(lnkname,0)  

if __name__ == '__main__':
    createDesktopLnk(u"C:\Python27\python.exe","MyPython")

 

 

例子二:
首先得安装 ActiveState ActivePython  . 因为这个中带了 winshell 库

代码如下:

from os import path    
import winshell    

  
#----------------------------------------------------------------------  
def create_shortcut_to_desktop(target,title):  
    """Create shortcut to desktop"""  
    s = path.basename(target)    
    fname = path.splitext(s)[0]    
    winshell.CreateShortcut(    
    Path = path.join(winshell.desktop(), fname + '.lnk'),    
    Target = target,    
    Icon=(target, 0),    
    Description=title)  


注:不支持win64

 


二、使用winshell模块创建、删除桌面、启动组快捷方式

当写好一个应用并发行的时候,我们希望在用户的桌面上建立快捷方式方便用户操作,winshell模块提供了我们需要的功能

下面这个函数将创建程序自身的快捷方式到桌面:

代码如下:

from os import path  
import winshell  

def create_shortcut_to_desktop():  
    target = argv[0]  
    title = '我的快捷方式' 
    s = path.basename(target)  
    fname = path.splitext(s)[0]  
    winshell.CreateShortcut(  
    Path = path.join(winshell.desktop(), fname + '.lnk'),  
    Target = target,  
    Icon=(target, 0),  
    Description=title)  

 

下面这个函数实现将本程序的快捷方式从桌面删除:

 

代码如下:

 def delete_shortcut_from_startup():  
    target = argv[0]  
    s = path.basename(target)  
    fname = path.splitext(s)[0]  
    delfile = path.join(winshell.startup(), fname + '.lnk')  
    winshell.delete_file(delfile) 
 

 

下面这个函数实现了建立快捷方式到启动组:

代码如下:

from os import path  
import winshell  

def create_shortcut_to_startup():  
      target = argv[0]  
      title = '我的快捷方式' 
      s = path.basename(target)  
      fname = path.splitext(s)[0]  
      winshell.CreateShortcut(  
      Path = path.join(winshell.startup(),   
      fname + '.lnk'),  
      Target = target,  
      Icon=(target, 0),  
      Description=title) 
posted on   slqt  阅读(3533)  评论(0编辑  收藏  举报
编辑推荐:
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
阅读排行:
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· 深度对比:PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
 
点击右上角即可分享
微信分享提示