python:wxpython

文書の過去の版を表示しています。


wxPython

wxPython Logo
本家: Welcome to wxPython! | wxPython
ソースコード:
wxWidgets/Phoenix: wxPython's Project Phoenix. A new implementation of wxPython, better, stronger, faster than he was before. (wxPython Phoenix 4系)
wxWidgets/wxPython-Classic: This is the repository for "Classic" wxPython. All new development is happening in the Phoenix project at https://github.com/wxWidgets/Phoenix (wxPython Classic 3系)
リリース:
wxPython · PyPI (wxPython Phoenix 4系)
wxPython - Browse /wxPython at SourceForge.net (wxPython Classic 3系)

 wxPythonは、wxWidgets C++ クラス ライブラリ(クロス プラットフォーム GUIライブラリ)のPython言語向けバインディングである。つまり、PythonからwxWidgetsを利用できるようにしたものである。
 wxPythonを使いPythonでプログラムを記述すると、スクリプト言語でGUIアプリケーションを作成できる。さらに、そのプログラムはLinuxやWindows、Mac OS Xで同じように動作する。(PythonとwxPythonが利用できる様々なプラットフォームで動作する。)

wxPython Phoenix 4

PS > pip install wxpython
Collecting wxpython
  Downloading wxPython-4.0.7.post2-cp38-cp38-win_amd64.whl (22.9 MB)
     |████████████████████████████████| 22.9 MB 819 kB/s
Collecting six
  Downloading six-1.14.0-py2.py3-none-any.whl (10 kB)
Collecting numpy; python_version >= "3.0"
  Downloading numpy-1.18.1-cp38-cp38-win_amd64.whl (12.8 MB)
     |████████████████████████████████| 12.8 MB 1.3 MB/s
Collecting pillow
  Downloading Pillow-7.0.0-cp38-cp38-win_amd64.whl (2.0 MB)
     |████████████████████████████████| 2.0 MB 1.3 MB/s
Installing collected packages: six, numpy, pillow, wxpython
Successfully installed numpy-1.18.1 pillow-7.0.0 six-1.14.0 wxpython-4.0.7.post2

フォルダ構成を以下のようにする。

build_folder
+ wxWidgets
+ wxPython-Classic

wxWidgets をダウンロードする。
GitHub - wxWidgets/wxWidgets: Cross-Platform GUI Library - Report issues here: https://trac.wxwidgets.org/ より clone する。

> git clone --recurse-submodules https://github.com/wxWidgets/wxWidgets.git

–recurse-submodules 無しで clone してしまった場合は以下のコマンドを実行する。

> cd wxWidgets
> git submodule update --init

v3.0.2 タグをチェックアウトする。

> cd wxWidgets
> git checkout -f v3.0.2

wxPython をダウンロードする。

> git clone https://github.com/wxWidgets/wxPython-Classic.git

wxPy-3.0.2.0 タグをチェックアウトする。

> cd wxPython-Classic
> git checkout -f wxPy-3.0.2.0

wxPython をビルドする。
Python 2.7 向けにビルドする場合
予め Microsoft Visual C++ Compiler Pack for Python 2.7 のインストールが必要である。

> cd wxPython-Classic\wxPython

x32 ビルド

> "%LOCALAPPDATA%\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat" x86
Setting environment for using Microsoft Visual Studio 2008 x86 tools.
> set CPU=x86
> py -2 build-wxpython.py

x64 ビルド

> "%LOCALAPPDATA%\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat" x64
Setting environment for using Microsoft Visual Studio 2008 x64 tools.
> set CPU=x64
> py -2 build-wxpython.py

フォルダ構成を以下のようにする。

build_folder
+ wxWidgets
+ wxPython-Phoenix
> git clone https://github.com/wxWidgets/Phoenix.git wxPython-Phoenix
> "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.4.5
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

link.EXE : return code '0x458' が発生する場合

vcvarsall.bat の引数 x86/x64 を適切に指定する。

> "%LOCALAPPDATA%\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat" x86
Setting environment for using Microsoft Visual Studio 2008 x86 tools.

or

> "%LOCALAPPDATA%\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat" x64
Setting environment for using Microsoft Visual Studio 2008 x64 tools.

error: Microsoft Visual C++ 9.0 is required. が発生する場合

以下のエラーが発生する場合

error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27
ERROR: failed building wxPython.

Download Microsoft Visual C++ Compiler for Python 2.7 from Official Microsoft Download Center より VCForPython27.msi をダウンロードする。
VCForPython27.msi を実行してライセンスに同意してインストールする。
以下のコマンドを実行して Microsoft Visual C++ Compiler Pack for Python 2.7 環境を有効にする。

> "%LOCALAPPDATA%\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat"
Setting environment for using Microsoft Visual Studio 2008 x86 tools.

error: Unable to find vcvarsall.bat が発生する場合

以下のエラーが発生する場合

error: Unable to find vcvarsall.bat
ERROR: failed building wxPython.
> set VS90COMNTOOLS="%LOCALAPPDATA%\Programs\Common\Microsoft\Visual C++ for Python\9.0"

Test

> py -2
Python 2.7.17 (v2.7.17:c2f86d86e6, Oct 19 2019, 21:01:17) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from distutils.msvccompiler import get_build_version
>>> version = get_build_version()
>>> version
9.0
>>> from distutils.msvc9compiler import *
>>> find_vcvarsall(version)
>>> query_vcvarsall(version, 'x64')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\distutils\msvc9compiler.py", line 271, in query_vcvarsall
    raise DistutilsPlatformError("Unable to find vcvarsall.bat")
distutils.errors.DistutilsPlatformError: Unable to find vcvarsall.bat
>>> from setuptools import setup
>>> from distutils.msvc9compiler import *
>>> find_vcvarsall(version)
u'C:\\Users\\tomoyan\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\vcvarsall.bat'
>>> ^Z

setuptoolsimport により monkey patch が当たり Visual C++ for Python が検出される。

UTF-8で保存すること。

PyWxHelloMain.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx
 
# PyWxHelloFrame クラス(wxFrame から派生)
class PyWxHelloFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        # Panel 生成
        panel = wx.Panel(self, -1)
        # Panel に [閉じる] ボタンを追加
        btnClose = wx.Button(panel, wx.NewId(), u"閉じる")
        # ボタンの位置を設定
        btnClose.SetPosition((50, 35))
        # クリックイベントにイベントハンドラをバインド
        self.Bind(wx.EVT_BUTTON, self.btnClose_Click, btnClose)
 
    # [閉じる] ボタンクリック時
    def btnClose_Click(self, event):
        # 閉じる
        self.Close(True)
 
# PyWxHelloApp クラス(wxApp から派生)
class PyWxHelloApp(wx.App):
    def OnInit(self):
        # PyWxHelloFrame クラスを生成(タイトル、サイズを指定)
        mainFrame = PyWxHelloFrame(None, -1, "Hello wxPython!!", size=(200, 100))
        # トップウインドウとして設定
        self.SetTopWindow(mainFrame)
        # 表示
        mainFrame.Show(True)
        # 正常終了
        return True
 
# メイン処理
def main():
    # PyWxHelloApp クラスを生成
    app = PyWxHelloApp(False)
    # メインループを開始
    app.MainLoop()
 
# スクリプト起動時にmain()を実行
if __name__ == "__main__":
    main()
PyWxWebView.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx
import wx.html2
 
# PyWxWebViewDialog クラス(wxDialog から派生)
class PyWxWebViewDialog(wx.Dialog):
    def __init__(self, *args, **kwds):
        wx.Dialog.__init__(self, *args, **kwds)
        # BoxSizer 生成
        sizer = wx.BoxSizer(wx.VERTICAL)
        # WebView 生成
        self.browser = wx.html2.WebView.New(self)
        # BoxSizer に WebView を追加
        sizer.Add(self.browser, 1, wx.EXPAND, 10)
        # Dialog に BoxSizer レイアウトを適用
        self.SetSizer(sizer)
        # Dialog サイズを設定
        self.SetSize((1024, 700))
        # Dialog 最小サイズを設定
        self.SetMinSize((600,300))
        # Dialog をディスプレイ中央に配置
        self.Center()
 
if __name__ == '__main__':
    # wxApp クラスを生成
    app = wx.App()
    # PyWxWebViewDialog クラスを生成
    dialog = PyWxWebViewDialog(None, -1)
    # WebView の URL を設定
    dialog.browser.LoadURL('https://html5test.com/')
    # Dialog を表示
    dialog.Show()
    # メインループを開始
    app.MainLoop()

WebView は WebBrowser コントロール で実装されている。
HTML5test 確認すると Internet Explorer 11.0 相当であることがわかる。

wxWidgets\src\msw\webview_ie.cpp
bool wxWebViewIE::Create(wxWindow* parent,
           wxWindowID id,
           const wxString& url,
           const wxPoint& pos,
           const wxSize& size,
           long style,
           const wxString& name)
{
    if (!wxControl::Create(parent, id, pos, size, style,
                           wxDefaultValidator, name))
    {
        return false;
    }
 
    m_webBrowser = NULL;
    m_isBusy = false;
    m_historyLoadingFromList = false;
    m_historyEnabled = true;
    m_historyPosition = -1;
    m_zoomType = wxWEBVIEW_ZOOM_TYPE_TEXT;
    FindClear();
 
    if (::CoCreateInstance(CLSID_WebBrowser, NULL,
                           CLSCTX_INPROC_SERVER, // CLSCTX_INPROC,
                           IID_IWebBrowser2 , (void**)&m_webBrowser) != 0)
    {
        wxLogError("Failed to initialize IE, CoCreateInstance returned an error");
        return false;
    }
 
    m_ie.SetDispatchPtr(m_webBrowser); // wxAutomationObject will release itself
 
    m_webBrowser->put_RegisterAsBrowser(VARIANT_TRUE);
    m_webBrowser->put_RegisterAsDropTarget(VARIANT_TRUE);
 
    m_uiHandler = new DocHostUIHandler(this);
 
    m_container = new wxIEContainer(this, IID_IWebBrowser2, m_webBrowser, m_uiHandler);
 
    EnableControlFeature(21 /* FEATURE_DISABLE_NAVIGATION_SOUNDS */);
 
    LoadURL(url);
    return true;
}

参考文献

  • python/wxpython.1583542208.txt.gz
  • 最終更新: 2020/03/07 09:50
  • by ともやん