差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン両方とも次のリビジョン | ||
python:wxpython [2020/03/06 03:03] – [インストール] ともやん | python:wxpython [2021/06/11 12:33] – ともやん | ||
---|---|---|---|
行 1: | 行 1: | ||
- | < | + | |
- | < | + | |
- | #result pre { | + | |
- | overflow: scroll; | + | |
- | overflow-x: hidden; | + | |
- | font-size: 12px; | + | |
- | } | + | |
- | # | + | |
- | height: 300px; | + | |
- | overflow: scroll; | + | |
- | overflow-x: hidden; | + | |
- | font-size: 12px; | + | |
- | } | + | |
- | </ | + | |
- | </ | + | |
====== wxPython ====== | ====== wxPython ====== | ||
- | {{:python: | + | {{python: |
本家: [[https:// | 本家: [[https:// | ||
ソースコード: | ソースコード: | ||
- | ・[[https:// | + | ・[[git>wxWidgets/ |
- | ・[[https:// | + | ・[[git>wxWidgets/ |
リリース: | リリース: | ||
・[[https:// | ・[[https:// | ||
- | ・[[https:// | + | ・[[sf>wxpython/ |
\\ | \\ | ||
- | wxPythonは、wxWidgets C++ クラス ライブラリ(クロス プラットフォーム GUIライブラリ)のPython言語向けバインディングである。つまり、PythonからwxWidgetsを利用できるようにしたものである。\\ | + | wxPythonは、wxWidgets C++ クラス ライブラリ(クロス プラットフォーム GUI ライブラリ)の Python 言語向けバインディングである。つまり、Python から wxWidgets を利用できるようにしたものである。\\ |
- | wxPythonを使いPythonでプログラムを記述すると、スクリプト言語でGUIアプリケーションを作成できる。さらに、そのプログラムはLinuxやWindows、Mac OS Xで同じように動作する。(PythonとwxPythonが利用できる様々なプラットフォームで動作する。) | + | wxPython を使い Python でプログラムを記述すると、スクリプト言語で GUI アプリケーションを作成できる。さらに、そのプログラムは Linux や Windows、Mac OS Xで同じように動作する。(Python と wxPython が利用できる様々なプラットフォームで動作する。) |
===== インストール ===== | ===== インストール ===== | ||
- | wxPython Phoenix 4系\\ | + | **wxPython Phoenix 4**系\\ |
<code powershell> | <code powershell> | ||
PS > pip install wxpython | PS > pip install wxpython | ||
行 51: | 行 37: | ||
===== ビルドする場合 ===== | ===== ビルドする場合 ===== | ||
+ | |||
+ | ==== wxPython Classic 3系 ==== | ||
フォルダ構成を以下のようにする。\\ | フォルダ構成を以下のようにする。\\ | ||
^ build_folder ^^ | ^ build_folder ^^ | ||
行 70: | 行 58: | ||
> cd wxWidgets | > cd wxWidgets | ||
> git checkout -f v3.0.2 | > git checkout -f v3.0.2 | ||
+ | > git submodule update --init | ||
+ | > git status | ||
+ | HEAD detached at v3.0.2 | ||
+ | nothing to commit, working tree clean | ||
</ | </ | ||
行 86: | 行 78: | ||
予め [[http:// | 予め [[http:// | ||
< | < | ||
- | > cd wxPython-Classic | + | > cd wxPython-Classic\wxPython |
- | > " | + | |
- | Setting environment for using Microsoft Visual Studio 2008 x86 tools. | + | |
</ | </ | ||
x32 ビルド\\ | x32 ビルド\\ | ||
< | < | ||
- | > set CPU= | + | > " |
- | > python | + | Setting environment for using Microsoft Visual Studio 2008 x86 tools. |
+ | > set CPU=x86 | ||
+ | > py -2 build-wxpython.py | ||
</ | </ | ||
x64 ビルド\\ | x64 ビルド\\ | ||
< | < | ||
- | > set CPU=X64 | + | > " |
- | > python | + | Setting environment for using Microsoft Visual Studio 2008 x64 tools. |
+ | > set CPU=x64 | ||
+ | > py -2 build-wxpython.py | ||
+ | </ | ||
+ | |||
+ | ==== wxPython Phoenix 4系 ==== | ||
+ | フォルダ構成を以下のようにする。\\ | ||
+ | ^ build_folder ^^ | ||
+ | | + ^ wxWidgets ^ | ||
+ | | + ^ wxPython-Phoenix ^ | ||
+ | |||
+ | < | ||
+ | > git clone https:// | ||
+ | > cd wxPython-Phoenix | ||
+ | > " | ||
+ | ********************************************************************** | ||
+ | ** Visual Studio 2019 Developer Command Prompt v16.4.5 | ||
+ | ** Copyright (c) 2019 Microsoft Corporation | ||
+ | ********************************************************************** | ||
+ | [vcvarsall.bat] Environment initialized for: ' | ||
+ | > echo %VCToolsVersion% | ||
+ | 14.16.27023 | ||
+ | > git submodule update --init --recursive | ||
+ | </ | ||
+ | |||
+ | ==== トラブルシューティング ==== | ||
+ | |||
+ | === link.EXE : return code ' | ||
+ | **vcvarsall.bat** の引数 **x86/x64** を適切に指定する。\\ | ||
+ | < | ||
+ | > " | ||
+ | Setting environment for using Microsoft Visual Studio 2008 x86 tools. | ||
+ | </ | ||
+ | or | ||
+ | < | ||
+ | > " | ||
+ | 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. が発生する場合 === |
以下のエラーが発生する場合\\ | 以下のエラーが発生する場合\\ | ||
< | < | ||
行 116: | 行 144: | ||
</ | </ | ||
- | ==== error: Unable to find vcvarsall.bat が発生する場合 | + | === error: Unable to find vcvarsall.bat が発生する場合 === |
以下のエラーが発生する場合\\ | 以下のエラーが発生する場合\\ | ||
< | < | ||
行 128: | 行 156: | ||
Test\\ | Test\\ | ||
<code pyhton> | <code pyhton> | ||
- | > python | + | > py -2 |
Python 2.7.17 (v2.7.17: | Python 2.7.17 (v2.7.17: | ||
Type " | Type " | ||
>>> | >>> | ||
- | >>> | + | >>> |
+ | >>> | ||
9.0 | 9.0 | ||
>>> | >>> | ||
- | >>> | + | >>> |
- | >>> | + | >>> |
Traceback (most recent call last): | Traceback (most recent call last): | ||
File "< | File "< | ||
- | File "c:\python27\Lib\distutils\msvc9compiler.py", | + | File "C:\Python27\lib\distutils\msvc9compiler.py", |
raise DistutilsPlatformError(" | raise DistutilsPlatformError(" | ||
distutils.errors.DistutilsPlatformError: | distutils.errors.DistutilsPlatformError: | ||
>>> | >>> | ||
- | >>> | + | >>> |
- | >>> | + | >>> |
- | {u' | + | u' |
- | + | ||
- | | + | |
- | + | ||
- | | + | |
- | + | ||
- | | + | |
>>> | >>> | ||
</ | </ | ||
行 162: | 行 185: | ||
[[https:// | [[https:// | ||
- | ===== 最小のwxPythonアプリケーション ===== | + | ==== distutils.msvccompiler |
- | [PyWxHelloMain] - UTF-8で保存すること。 | + | <WRAP prewrap 100% #result> |
<code python> | <code python> | ||
+ | def get_build_version(): | ||
+ | """ | ||
+ | |||
+ | For Python 2.3 and up, the version number is included in | ||
+ | sys.version. | ||
+ | """ | ||
+ | prefix = "MSC v." | ||
+ | i = sys.version.find(prefix) | ||
+ | if i == -1: | ||
+ | return 6 | ||
+ | i = i + len(prefix) | ||
+ | s, rest = sys.version[i: | ||
+ | majorVersion = int(s[:-2]) - 6 | ||
+ | if majorVersion >= 13: | ||
+ | # v13 was skipped and should be v14 | ||
+ | majorVersion += 1 | ||
+ | minorVersion = int(s[2:3]) / 10.0 | ||
+ | # I don't think paths are affected by minor version in version 6 | ||
+ | if majorVersion == 6: | ||
+ | minorVersion = 0 | ||
+ | if majorVersion >= 6: | ||
+ | return majorVersion + minorVersion | ||
+ | # else we don't know what version of the compiler this is | ||
+ | return None | ||
+ | </ | ||
+ | </ | ||
+ | <WRAP prewrap 100% #result> | ||
+ | <code python> | ||
+ | >>> | ||
+ | >>> | ||
+ | 14.1 | ||
+ | >>> | ||
+ | >>> | ||
+ | '3.8.1 (tags/ | ||
+ | >>> | ||
+ | ' | ||
+ | >>> | ||
+ | 13 >>> | ||
+ | >>> | ||
+ | 14 | ||
+ | >>> | ||
+ | ' | ||
+ | >>> | ||
+ | >>> | ||
+ | 0.1 | ||
+ | >>> | ||
+ | 14.1 | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ==== distutils.msvc9compiler の find_vcvarsall() の実装 (Python 3.8) ==== | ||
+ | <WRAP prewrap 100% #result> | ||
+ | <code python> | ||
+ | def find_vcvarsall(version): | ||
+ | """ | ||
+ | |||
+ | At first it tries to find the productdir of VS 2008 in the registry. If | ||
+ | that fails it falls back to the VS90COMNTOOLS env var. | ||
+ | """ | ||
+ | vsbase = VS_BASE % version | ||
+ | try: | ||
+ | productdir = Reg.get_value(r" | ||
+ | " | ||
+ | except KeyError: | ||
+ | log.debug(" | ||
+ | productdir = None | ||
+ | |||
+ | if not productdir or not os.path.isdir(productdir): | ||
+ | toolskey = " | ||
+ | toolsdir = os.environ.get(toolskey, | ||
+ | |||
+ | if toolsdir and os.path.isdir(toolsdir): | ||
+ | productdir = os.path.join(toolsdir, | ||
+ | productdir = os.path.abspath(productdir) | ||
+ | if not os.path.isdir(productdir): | ||
+ | log.debug(" | ||
+ | return None | ||
+ | else: | ||
+ | log.debug(" | ||
+ | if not productdir: | ||
+ | log.debug(" | ||
+ | return None | ||
+ | vcvarsall = os.path.join(productdir, | ||
+ | if os.path.isfile(vcvarsall): | ||
+ | return vcvarsall | ||
+ | log.debug(" | ||
+ | return None | ||
+ | </ | ||
+ | </ | ||
+ | <WRAP prewrap 100% #result> | ||
+ | <code python> | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== 最小のwxPythonアプリケーション ===== | ||
+ | UTF-8で保存すること。 | ||
+ | <WRAP prewrap 100% # | ||
+ | <code python PyWxHelloMain.py> | ||
# | # | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
- | |||
import wx | import wx | ||
- | # PyWxHelloFrameクラス(wxFrameから派生) | + | # PyWxHelloFrame クラス(wxFrame から派生) |
class PyWxHelloFrame(wx.Frame): | class PyWxHelloFrame(wx.Frame): | ||
def __init__(self, | def __init__(self, | ||
wx.Frame.__init__(self, | wx.Frame.__init__(self, | ||
- | # Panel生成 | + | # Panel 生成 |
panel = wx.Panel(self, | panel = wx.Panel(self, | ||
- | # Panelに[閉じる]ボタンを追加 | + | # Panel に [閉じる] ボタンを追加 |
btnClose = wx.Button(panel, | btnClose = wx.Button(panel, | ||
# ボタンの位置を設定 | # ボタンの位置を設定 | ||
行 183: | 行 303: | ||
self.Bind(wx.EVT_BUTTON, | self.Bind(wx.EVT_BUTTON, | ||
- | # [閉じる]ボタンクリック時 | + | # [閉じる] ボタンクリック時 |
def btnClose_Click(self, | def btnClose_Click(self, | ||
# 閉じる | # 閉じる | ||
self.Close(True) | self.Close(True) | ||
- | # PyWxHelloAppクラス(wxAppから派生) | + | # PyWxHelloApp クラス(wxApp から派生) |
class PyWxHelloApp(wx.App): | class PyWxHelloApp(wx.App): | ||
def OnInit(self): | def OnInit(self): | ||
- | # PyWxHelloFrameクラスを生成(タイトル、サイズを指定) | + | # PyWxHelloFrame クラスを生成(タイトル、サイズを指定) |
mainFrame = PyWxHelloFrame(None, | mainFrame = PyWxHelloFrame(None, | ||
# トップウインドウとして設定 | # トップウインドウとして設定 | ||
行 202: | 行 322: | ||
# メイン処理 | # メイン処理 | ||
def main(): | def main(): | ||
- | # PyWxHelloAppクラスを生成 | + | # PyWxHelloApp クラスを生成 |
app = PyWxHelloApp(False) | app = PyWxHelloApp(False) | ||
# メインループを開始 | # メインループを開始 | ||
行 211: | 行 331: | ||
main() | main() | ||
</ | </ | ||
+ | </ | ||
+ | |||
+ | <WRAP prewrap 100% # | ||
+ | <code python PyWxWebView.py> | ||
+ | # | ||
+ | # -*- coding: utf-8 -*- | ||
+ | import wx | ||
+ | import wx.html2 | ||
+ | |||
+ | # PyWxWebViewDialog クラス(wxDialog から派生) | ||
+ | class PyWxWebViewDialog(wx.Dialog): | ||
+ | def __init__(self, | ||
+ | wx.Dialog.__init__(self, | ||
+ | # BoxSizer 生成 | ||
+ | sizer = wx.BoxSizer(wx.VERTICAL) | ||
+ | # WebView 生成 | ||
+ | self.browser = wx.html2.WebView.New(self) | ||
+ | # BoxSizer に WebView を追加 | ||
+ | sizer.Add(self.browser, | ||
+ | # Dialog に BoxSizer レイアウトを適用 | ||
+ | self.SetSizer(sizer) | ||
+ | # Dialog サイズを設定 | ||
+ | self.SetSize((1024, | ||
+ | # Dialog 最小サイズを設定 | ||
+ | self.SetMinSize((600, | ||
+ | # Dialog をディスプレイ中央に配置 | ||
+ | self.Center() | ||
+ | |||
+ | if __name__ == ' | ||
+ | # wxApp クラスを生成 | ||
+ | app = wx.App() | ||
+ | # PyWxWebViewDialog クラスを生成 | ||
+ | dialog = PyWxWebViewDialog(None, | ||
+ | # WebView の URL を設定 | ||
+ | dialog.browser.LoadURL(' | ||
+ | # Dialog を表示 | ||
+ | dialog.Show() | ||
+ | # メインループを開始 | ||
+ | app.MainLoop() | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== 技術情報 ===== | ||
+ | |||
+ | ==== WebView ==== | ||
+ | WebView は [[https:// | ||
+ | [[https:// | ||
+ | <WRAP prewrap 100% # | ||
+ | <code cpp wxWidgets\src\msw\webview_ie.cpp> | ||
+ | bool wxWebViewIE:: | ||
+ | | ||
+ | const wxString& | ||
+ | const wxPoint& | ||
+ | const wxSize& size, | ||
+ | long style, | ||
+ | const wxString& | ||
+ | { | ||
+ | if (!wxControl:: | ||
+ | | ||
+ | { | ||
+ | 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 (:: | ||
+ | | ||
+ | | ||
+ | { | ||
+ | wxLogError(" | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | m_ie.SetDispatchPtr(m_webBrowser); | ||
+ | |||
+ | m_webBrowser-> | ||
+ | m_webBrowser-> | ||
+ | |||
+ | m_uiHandler = new DocHostUIHandler(this); | ||
+ | |||
+ | m_container = new wxIEContainer(this, | ||
+ | |||
+ | EnableControlFeature(21 /* FEATURE_DISABLE_NAVIGATION_SOUNDS */); | ||
+ | |||
+ | LoadURL(url); | ||
+ | return true; | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== 参考文献 ===== | ||
+ | [[https:// | ||
+ | [[http:// | ||
+ | [[https:// | ||
+ | [[https:// | ||
+ | [[https:// | ||
+ | [[https:// | ||