Существует ли межплатформенный способ открыть файловый браузер в Python?

Для окончания этого вопроса вот, класс помощника, который нужно назвать, когда форма загружается для каждого ListView или полученного управления любого другого ListView в форме. Благодаря "Brian Gillespie" для предоставления решения.

public enum ListViewExtendedStyles
{
    /// <summary>
    /// LVS_EX_GRIDLINES
    /// </summary>
    GridLines = 0x00000001,
    /// <summary>
    /// LVS_EX_SUBITEMIMAGES
    /// </summary>
    SubItemImages = 0x00000002,
    /// <summary>
    /// LVS_EX_CHECKBOXES
    /// </summary>
    CheckBoxes = 0x00000004,
    /// <summary>
    /// LVS_EX_TRACKSELECT
    /// </summary>
    TrackSelect = 0x00000008,
    /// <summary>
    /// LVS_EX_HEADERDRAGDROP
    /// </summary>
    HeaderDragDrop = 0x00000010,
    /// <summary>
    /// LVS_EX_FULLROWSELECT
    /// </summary>
    FullRowSelect = 0x00000020,
    /// <summary>
    /// LVS_EX_ONECLICKACTIVATE
    /// </summary>
    OneClickActivate = 0x00000040,
    /// <summary>
    /// LVS_EX_TWOCLICKACTIVATE
    /// </summary>
    TwoClickActivate = 0x00000080,
    /// <summary>
    /// LVS_EX_FLATSB
    /// </summary>
    FlatsB = 0x00000100,
    /// <summary>
    /// LVS_EX_REGIONAL
    /// </summary>
    Regional = 0x00000200,
    /// <summary>
    /// LVS_EX_INFOTIP
    /// </summary>
    InfoTip = 0x00000400,
    /// <summary>
    /// LVS_EX_UNDERLINEHOT
    /// </summary>
    UnderlineHot = 0x00000800,
    /// <summary>
    /// LVS_EX_UNDERLINECOLD
    /// </summary>
    UnderlineCold = 0x00001000,
    /// <summary>
    /// LVS_EX_MULTIWORKAREAS
    /// </summary>
    MultilWorkAreas = 0x00002000,
    /// <summary>
    /// LVS_EX_LABELTIP
    /// </summary>
    LabelTip = 0x00004000,
    /// <summary>
    /// LVS_EX_BORDERSELECT
    /// </summary>
    BorderSelect = 0x00008000,
    /// <summary>
    /// LVS_EX_DOUBLEBUFFER
    /// </summary>
    DoubleBuffer = 0x00010000,
    /// <summary>
    /// LVS_EX_HIDELABELS
    /// </summary>
    HideLabels = 0x00020000,
    /// <summary>
    /// LVS_EX_SINGLEROW
    /// </summary>
    SingleRow = 0x00040000,
    /// <summary>
    /// LVS_EX_SNAPTOGRID
    /// </summary>
    SnapToGrid = 0x00080000,
    /// <summary>
    /// LVS_EX_SIMPLESELECT
    /// </summary>
    SimpleSelect = 0x00100000
}

public enum ListViewMessages
{
    First = 0x1000,
    SetExtendedStyle = (First + 54),
    GetExtendedStyle = (First + 55),
}

/// <summary>
/// Contains helper methods to change extended styles on ListView, including enabling double buffering.
/// Based on Giovanni Montrone's article on <see cref="http://www.codeproject.com/KB/list/listviewxp.aspx"/>
/// </summary>
public class ListViewHelper
{
    private ListViewHelper()
    {
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int SendMessage(IntPtr handle, int messg, int wparam, int lparam);

    public static void SetExtendedStyle(Control control, ListViewExtendedStyles exStyle)
    {
        ListViewExtendedStyles styles;
        styles = (ListViewExtendedStyles)SendMessage(control.Handle, (int)ListViewMessages.GetExtendedStyle, 0, 0);
        styles |= exStyle;
        SendMessage(control.Handle, (int)ListViewMessages.SetExtendedStyle, 0, (int)styles);
    }

    public static void EnableDoubleBuffer(Control control)
    {
        ListViewExtendedStyles styles;
        // read current style
        styles = (ListViewExtendedStyles)SendMessage(control.Handle, (int)ListViewMessages.GetExtendedStyle, 0, 0);
        // enable double buffer and border select
        styles |= ListViewExtendedStyles.DoubleBuffer | ListViewExtendedStyles.BorderSelect;
        // write new style
        SendMessage(control.Handle, (int)ListViewMessages.SetExtendedStyle, 0, (int)styles);
    }
    public static void DisableDoubleBuffer(Control control)
    {
        ListViewExtendedStyles styles;
        // read current style
        styles = (ListViewExtendedStyles)SendMessage(control.Handle, (int)ListViewMessages.GetExtendedStyle, 0, 0);
        // disable double buffer and border select
        styles -= styles & ListViewExtendedStyles.DoubleBuffer;
        styles -= styles & ListViewExtendedStyles.BorderSelect;
        // write new style
        SendMessage(control.Handle, (int)ListViewMessages.SetExtendedStyle, 0, (int)styles);
    }
}
12
задан cdleary 25 November 2009 в 06:56
поделиться

3 ответа

I'd prefer not to kludge it up if I can avoid it.

Weeell I think you are going to need a little bit of platform-sniffing kludge, but hopefully not as much as the ghastly command-sniffing webbrowser module. Here's a first stab at it:

if sys.platform=='win32':
    subprocess.Popen(['start', d], shell= True)

elif sys.platform=='darwin':
    subprocess.Popen(['open', d])

else:
    try:
        subprocess.Popen(['xdg-open', d])
    except OSError:
        # er, think of something else to try
        # xdg-open *should* be supported by recent Gnome, KDE, Xfce

Note the win32 version will currently fail for spaces in filenames. Bug 2304 might be something to do with that, but there does seem to be a basic problem with parameter escaping and the Windows shell (cmd /c ...), in that you can't nest double-quotes and you can't ^-escape quotes or spaces. I haven't managed to find any way to quote and run cmd /c start C:\Documents and Settings from the command line at all.

ETA re nosklo's comment: on Windows only, there is a built-in way to do it:

if sys.platform=='win32':
    os.startfile(d)

Here's the not-very-nice alternative solution to find the shell and open a folder with it, which you shouldn't now need, but I'll leave in. (Partly because it might be of use for something else, but mostly because I spent the time to type the damned thing!)

if sys.platform=='win32':
    import _winreg
    path= r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon')
    for root in (_winreg.HKEY_CURRENT_USER, _winreg.HKEY_LOCAL_MACHINE):
        try:
            with _winreg.OpenKey(root, path) as k:
                value, regtype= _winreg.QueryValueEx(k, 'Shell')
        except WindowsError:
            pass
        else:
            if regtype in (_winreg.REG_SZ, _winreg.REG_EXPAND_SZ):
                shell= value
            break
    else:
        shell= 'Explorer.exe'
    subprocess.Popen([shell, d])
16
ответ дан 2 December 2019 в 21:03
поделиться

Это полный удар в темноте, но взгляните на wxPython , который обеспечивает привязку Python к базовой библиотеке wxWidgets . Прошло много времени с тех пор, как я в последний раз смотрел на нее, но, возможно, там есть что-то, что вы можете использовать. В противном случае будет относительно легко создать свой собственный файловый браузер, который будет использовать собственные «виджеты» для каждой ОС.

Имейте в виду, что wxPython не является легковесным, он действительно разгрузит ваше приложение и увеличит ваши зависимости.

1152771]

-1
ответ дан 2 December 2019 в 21:03
поделиться

Я не знаю, существует ли готовый к использованию модуль, но если он есть, он должен быть в python cookbok Activestate ( http: //code.activestate. com / recipes / langs / python / )

Кроме того, по крайней мере, в gnome и на mac os вы можете выполнить команды "gnome-open http: // blah " и "open http: // blah "(на Mac); оба откроют URL-адрес в предпочтительном браузере пользователя.

Для Linux также проверьте freedesktop.org - общий набор инструментов, охватывающий как Gnome, так и KDE, который должен включать что-то подобное.

-1
ответ дан 2 December 2019 в 21:03
поделиться
Другие вопросы по тегам:

Похожие вопросы: