差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン両方とも次のリビジョン | ||
python:wxpython [2020/03/07 09:08] – [wxPython Classic 3系] ともやん | python:wxpython [2021/06/12 11:52] – [最小の wxPython アプリケーション] ともやん | ||
---|---|---|---|
行 1: | 行 1: | ||
- | < | ||
- | < | ||
- | #result pre { | ||
- | overflow: scroll; | ||
- | overflow-x: hidden; | ||
- | font-size: 12px; | ||
- | } | ||
- | # | ||
- | height: 300px; | ||
- | overflow: scroll; | ||
- | overflow-x: hidden; | ||
- | font-size: 12px; | ||
- | } | ||
- | # | ||
- | 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 | + | $ pip install wxpython |
</ | </ | ||
<WRAP prewrap 100% #result> | <WRAP prewrap 100% #result> | ||
< | < | ||
Collecting wxpython | Collecting wxpython | ||
- | Downloading wxPython-4.0.7.post2-cp38-cp38-win_amd64.whl (22.9 MB) | + | Downloading wxPython-4.1.1-cp39-cp39-win_amd64.whl (18.1 MB) |
- | | + | |
Collecting six | Collecting six | ||
- | Downloading six-1.14.0-py2.py3-none-any.whl (10 kB) | + | Downloading six-1.16.0-py2.py3-none-any.whl (11 kB) |
- | Collecting numpy; python_version >= " | + | |
- | Downloading numpy-1.18.1-cp38-cp38-win_amd64.whl (12.8 MB) | + | |
- | | + | |
Collecting pillow | Collecting pillow | ||
- | Downloading Pillow-7.0.0-cp38-cp38-win_amd64.whl (2.0 MB) | + | Downloading Pillow-8.2.0-cp39-cp39-win_amd64.whl (2.2 MB) |
- | | + | |
- | Installing collected packages: six, numpy, pillow, wxpython | + | Collecting numpy; python_version >= " |
- | Successfully installed numpy-1.18.1 pillow-7.0.0 six-1.14.0 wxpython-4.0.7.post2 | + | Downloading numpy-1.20.3-cp39-cp39-win_amd64.whl (13.7 MB) |
+ | | ||
+ | Installing collected packages: six, pillow, numpy, wxpython | ||
+ | Successfully installed numpy-1.20.3 pillow-8.2.0 six-1.16.0 wxpython-4.1.1 | ||
</ | </ | ||
</ | </ | ||
行 77: | 行 57: | ||
> 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 | ||
</ | </ | ||
行 109: | 行 93: | ||
> set CPU=x64 | > set CPU=x64 | ||
> py -2 build-wxpython.py | > py -2 build-wxpython.py | ||
- | </ | ||
- | |||
- | === トラブルシューティング === | ||
- | |||
- | == 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: Unable to find vcvarsall.bat が発生する場合 == | ||
- | <code python> | ||
</ | </ | ||
行 137: | 行 103: | ||
< | < | ||
> git clone https:// | > git clone https:// | ||
- | > " | + | > cd wxPython-Phoenix |
+ | > " | ||
********************************************************************** | ********************************************************************** | ||
** Visual Studio 2019 Developer Command Prompt v16.4.5 | ** Visual Studio 2019 Developer Command Prompt v16.4.5 | ||
行 143: | 行 110: | ||
********************************************************************** | ********************************************************************** | ||
[vcvarsall.bat] Environment initialized for: ' | [vcvarsall.bat] Environment initialized for: ' | ||
+ | > echo %VCToolsVersion% | ||
+ | 14.16.27023 | ||
+ | > git submodule update --init --recursive | ||
</ | </ | ||
- | ==== error: Microsoft Visual C++ 9.0 is required. が発生する場合 | + | |
+ | ==== トラブルシューティング ==== | ||
+ | |||
+ | === 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. が発生する場合 === | ||
以下のエラーが発生する場合\\ | 以下のエラーが発生する場合\\ | ||
< | < | ||
行 158: | 行 143: | ||
</ | </ | ||
- | ==== error: Unable to find vcvarsall.bat が発生する場合 | + | === error: Unable to find vcvarsall.bat が発生する場合 === |
以下のエラーが発生する場合\\ | 以下のエラーが発生する場合\\ | ||
< | < | ||
行 170: | 行 155: | ||
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' |
- | + | ||
- | | + | |
- | + | ||
- | | + | |
- | + | ||
- | | + | |
>>> | >>> | ||
</ | </ | ||
行 204: | 行 184: | ||
[[https:// | [[https:// | ||
- | ===== 最小のwxPythonアプリケーション ===== | + | ==== distutils.msvccompiler の get_build_version() の実装 (Python 3.8) ==== |
- | UTF-8で保存すること。 | + | <WRAP prewrap 100% # |
+ | <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% # | ||
+ | <code python> | ||
+ | >>> | ||
+ | >>> | ||
+ | 14.1 | ||
+ | >>> | ||
+ | >>> | ||
+ | '3.8.1 (tags/ | ||
+ | >>> | ||
+ | ' | ||
+ | >>> | ||
+ | 13 >>> | ||
+ | >>> | ||
+ | 14 | ||
+ | >>> | ||
+ | ' | ||
+ | >>> | ||
+ | >>> | ||
+ | 0.1 | ||
+ | >>> | ||
+ | 14.1 | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ==== distutils.msvc9compiler の find_vcvarsall() の実装 (Python 3.8) ==== | ||
+ | <WRAP prewrap 100% # | ||
+ | <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 | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== 最小の wxPython アプリケーション ===== | ||
+ | <wrap em> | ||
+ | |||
+ | ==== wx.Frame サンプル ==== | ||
+ | {{: | ||
<WRAP prewrap 100% # | <WRAP prewrap 100% # | ||
<code python PyWxHelloMain.py> | <code python PyWxHelloMain.py> | ||
行 211: | 行 288: | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
import wx | import wx | ||
+ | |||
# PyWxHelloFrame クラス(wxFrame から派生) | # PyWxHelloFrame クラス(wxFrame から派生) | ||
class PyWxHelloFrame(wx.Frame): | class PyWxHelloFrame(wx.Frame): | ||
行 219: | 行 296: | ||
panel = wx.Panel(self, | panel = wx.Panel(self, | ||
# Panel に [閉じる] ボタンを追加 | # Panel に [閉じる] ボタンを追加 | ||
- | btnClose = wx.Button(panel, | + | btnClose = wx.Button(panel, |
# ボタンの位置を設定 | # ボタンの位置を設定 | ||
- | btnClose.SetPosition((50, 35)) | + | btnClose.SetPosition((100, 35)) |
# クリックイベントにイベントハンドラをバインド | # クリックイベントにイベントハンドラをバインド | ||
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, |
# トップウインドウとして設定 | # トップウインドウとして設定 | ||
self.SetTopWindow(mainFrame) | self.SetTopWindow(mainFrame) | ||
行 241: | 行 318: | ||
# 正常終了 | # 正常終了 | ||
return True | return True | ||
+ | |||
# メイン処理 | # メイン処理 | ||
def main(): | def main(): | ||
行 248: | 行 325: | ||
# メインループを開始 | # メインループを開始 | ||
app.MainLoop() | app.MainLoop() | ||
+ | |||
# スクリプト起動時にmain()を実行 | # スクリプト起動時にmain()を実行 | ||
if __name__ == " | if __name__ == " | ||
行 254: | 行 331: | ||
</ | </ | ||
</ | </ | ||
+ | |||
+ | サンプルアプリケーションの実行。\\ | ||
+ | <WRAP prewrap 100%> | ||
+ | < | ||
+ | $ python PyWxHelloMain.py | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ==== wx.html2.WebView サンプル ==== | ||
+ | {{: | ||
<WRAP prewrap 100% # | <WRAP prewrap 100% # | ||
行 261: | 行 348: | ||
import wx | import wx | ||
import wx.html2 | import wx.html2 | ||
- | + | ||
- | # PyWxWebViewDialog | + | # PyWxWebViewDialog (wxDialog ) |
class PyWxWebViewDialog(wx.Dialog): | class PyWxWebViewDialog(wx.Dialog): | ||
def __init__(self, | def __init__(self, | ||
wx.Dialog.__init__(self, | wx.Dialog.__init__(self, | ||
- | # BoxSizer | + | # BoxSizer |
sizer = wx.BoxSizer(wx.VERTICAL) | sizer = wx.BoxSizer(wx.VERTICAL) | ||
- | # WebView | + | # WebView |
- | self.browser = wx.html2.WebView.New(self) | + | self.browser = wx.html2.WebView.New(self, backend=wx.html2.WebViewBackendIE) |
- | # BoxSizer | + | # |
+ | # BoxSizer | ||
sizer.Add(self.browser, | sizer.Add(self.browser, | ||
- | # Dialog | + | # Dialog |
self.SetSizer(sizer) | self.SetSizer(sizer) | ||
- | # Dialog | + | # Dialog |
self.SetSize((1024, | self.SetSize((1024, | ||
- | # Dialog | + | # Dialog |
self.SetMinSize((600, | self.SetMinSize((600, | ||
- | # Dialog | + | # Dialog |
self.Center() | self.Center() | ||
+ | |||
if __name__ == ' | if __name__ == ' | ||
- | # wxApp クラスを生成 | + | # wxApp |
app = wx.App() | app = wx.App() | ||
- | # PyWxWebViewDialog | + | # PyWxWebViewDialog |
dialog = PyWxWebViewDialog(None, | dialog = PyWxWebViewDialog(None, | ||
- | # WebView | + | # WebView |
dialog.browser.LoadURL(' | dialog.browser.LoadURL(' | ||
- | # Dialog | + | # Dialog |
dialog.Show() | dialog.Show() | ||
- | # メインループを開始 | + | # |
app.MainLoop() | app.MainLoop() | ||
</ | </ | ||
+ | </ | ||
+ | |||
+ | サンプルアプリケーションの実行。\\ | ||
+ | <WRAP prewrap 100%> | ||
+ | < | ||
+ | $ python PyWxWebView.py</ | ||
</ | </ | ||
行 349: | 行 443: | ||
</ | </ | ||
- | === 参考文献 === | + | ===== 参考文献 ===== |
+ | [[https:// | ||
[[http:// | [[http:// | ||
[[https:// | [[https:// | ||
[[https:// | [[https:// | ||
[[https:// | [[https:// | ||
+ | [[https:// | ||
+ | |||
+ | ==== 付録 ==== | ||
+ | [[tw> | ||
+ | |||