2014-07-19 17 views
5

Tôi muốn tạo trình đơn bắt đầu hoặc Lối tắt trên màn hình cho gói trình cài đặt cửa sổ Python của mình. Tôi đang cố gắng theo dõi https://docs.python.org/3.4/distutils/builtdist.html#the-postinstallation-scriptCách tạo lối tắt trong menu startmenu bằng cách sử dụng trình cài đặt windowstools windows

Đây là tập lệnh của tôi;

import sys 

from os.path import dirname, join, expanduser 

pyw_executable = sys.executable.replace('python.exe','pythonw.exe') 
script_file = join(dirname(pyw_executable), 'Scripts', 'tklsystem-script.py') 
w_dir = expanduser(join('~','lsf_files')) 

print(sys.argv) 

if sys.argv[1] == '-install': 
    print('Creating Shortcut') 
    create_shortcut(
     target=pyw_executable, 
     description='A program to work with L-System Equations', 
     filename='L-System Tool', 
     arguments=script_file, 
     workdir=wdir 
    ) 

Tôi cũng xác định kịch bản này trong tùy chọn kịch bản cài đặt, như được chỉ ra bởi tài liệu nói trên.

Đây là lệnh tôi sử dụng để tạo trình cài đặt của mình;

python setup.py bdist_wininst --install-script tklsystem-post-install.py 

Sau khi cài đặt gói bằng trình cài đặt cửa sổ được tạo, tôi không thể xác định xem tập lệnh của mình có chạy hay không?

Làm cách nào để tôi có thể tạo trình cài đặt windows được tạo cho trình tạo cài đặt để tạo lối tắt trên màn hình hoặc trình đơn khởi động?

Trả lời

0

Nếu bạn muốn xác nhận xem tập lệnh có đang chạy hay không, bạn có thể in ra tệp thay vì bảng điều khiển. Có vẻ như văn bản bạn in cho bảng điều khiển trong tập lệnh sau khi cài đặt sẽ không hiển thị.

Hãy thử điều này:

import sys 
from os.path import expanduser, join 

pyw_executable = join(sys.prefix, "pythonw.exe") 
shortcut_filename = "L-System Toolsss.lnk" 
working_dir = expanduser(join('~','lsf_files')) 
script_path = join(sys.prefix, "Scripts", "tklsystem-script.py") 

if sys.argv[1] == '-install': 
    # Log output to a file (for test) 
    f = open(r"C:\test.txt",'w') 
    print('Creating Shortcut', file=f) 

    # Get paths to the desktop and start menu 
    desktop_path = get_special_folder_path("CSIDL_COMMON_DESKTOPDIRECTORY") 
    startmenu_path = get_special_folder_path("CSIDL_COMMON_STARTMENU") 

    # Create shortcuts. 
    for path in [desktop_path, startmenu_path]: 
     create_shortcut(pyw_executable, 
        "A program to work with L-System Equations", 
        join(path, shortcut_filename), 
        script_path, 
        working_dir) 
+0

Điều này thực sự làm việc với các mô-đun setuptools mới? Dường như ngay cả chức năng hỗ trợ get_special_folder_path cũng không tồn tại nữa. setuptools không thể thay thế hoàn toàn các distutils cũ. – LRMAAX

0

Giống như những người khác đã nhận xét ở đây và các nơi khác các chức năng hỗ trợ dường như không làm việc ở tất cả (ít nhất là không phải với setuptools). Sau một ngày tốt lành tìm kiếm thông qua các nguồn tài nguyên khác nhau, tôi đã tìm ra cách để tạo ít nhất phím tắt trên Màn hình nền. Tôi đang chia sẻ giải pháp của mình (về cơ bản là một hỗn hợp mã tôi tìm thấy herehere). Tôi nên thêm rằng trường hợp của tôi là hơi khác nhau từ yasar 's, bởi vì nó tạo ra một phím tắt cho một gói cài đặt (tức là một tập tin exe trong Python Scripts thư mục) thay vì một kịch bản.

Tóm lại, tôi đã thêm một hàm post_install vào setup.py của tôi và sau đó sử dụng Python extensions for Windows để tạo lối tắt. Vị trí của thư mục Desktop được đọc từ Windows registry (có nhiều phương pháp khác cho việc này, nhưng chúng có thể không đáng tin cậy nếu Desktop ở một vị trí không chuẩn).

#!/usr/bin/env python 

import os 
import sys 
import sysconfig 
if sys.platform == 'win32': 
    from win32com.client import Dispatch 
    import winreg 

def get_reg(name,path): 
    # Read variable from Windows Registry 
    # From https://stackoverflow.com/a/35286642 
    try: 
     registry_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, path, 0, 
             winreg.KEY_READ) 
     value, regtype = winreg.QueryValueEx(registry_key, name) 
     winreg.CloseKey(registry_key) 
     return value 
    except WindowsError: 
     return None 

def post_install(): 
    # Creates a Desktop shortcut to the installed software 

    # Package name 
    packageName = 'mypackage' 

    # Scripts directory (location of launcher script) 
    scriptsDir = sysconfig.get_path('scripts') 

    # Target of shortcut 
    target = os.path.join(scriptsDir, packageName + '.exe') 

    # Name of link file 
    linkName = packageName + '.lnk' 

    # Read location of Windows desktop folder from registry 
    regName = 'Desktop' 
    regPath = r'Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' 
    desktopFolder = os.path.normpath(get_reg(regName,regPath)) 

    # Path to location of link file 
    pathLink = os.path.join(desktopFolder, linkName) 
    shell = Dispatch('WScript.Shell') 
    shortcut = shell.CreateShortCut(pathLink) 
    shortcut.Targetpath = target 
    shortcut.WorkingDirectory = scriptsDir 
    shortcut.IconLocation = target 
    shortcut.save() 

setup(name='mypackage', 
     ..., 
     ...) 

if sys.argv[1] == 'install' and sys.platform == 'win32': 
    post_install() 

Dưới đây là một liên kết đến một kịch bản cài đặt đầy đủ trong đó tôi sử dụng này:

https://github.com/KBNLresearch/iromlab/blob/master/setup.py

Các vấn đề liên quan