Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cefpython/examples/wxpython.py at cefpython49-winxp · PythonNexus/cefpython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
PythonNexus
/
cefpython
Public
forked from
cztomczak/cefpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
cefpython49-winxp
Breadcrumbs
cefpython
/
examples
/
wxpython.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
236 lines (197 loc) · 7.89 KB
cefpython49-winxp
Breadcrumbs
cefpython
/
examples
/
wxpython.py
Copy path
Top
File metadata and controls
Code
Blame
236 lines (197 loc) · 7.89 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# Example of embedding CEF Python browser using wxPython library.
# This example has a top menu and a browser widget without navigation bar.
# Tested configurations:
# - wxPython 4.0 on Windows/Mac/Linux
# - wxPython 3.0 on Windows/Mac
# - wxPython 2.8 on Linux
# - CEF Python v49.0+
import
wx
from
cefpython3
import
cefpython
as
cef
import
platform
import
sys
import
os
# Fix for PyCharm hints warnings when using static methods
WindowUtils
=
cef
.
WindowUtils
()
# Platforms
WINDOWS
=
(
platform
.
system
()
==
"Windows"
)
LINUX
=
(
platform
.
system
()
==
"Linux"
)
MAC
=
(
platform
.
system
()
==
"Darwin"
)
# Configuration
WIDTH
=
800
HEIGHT
=
600
# Globals
g_count_windows
=
0
def
main
():
check_versions
()
sys
.
excepthook
=
cef
.
ExceptHook
# To shutdown all CEF processes on error
settings
=
{}
# To enable High DPI support embed a DPI awareness manifest in
# both the main process and the subprocess.exe executable. Calling
# cef.DpiAware.SetProcessDpiAware sets DPI awareness only for
# the main process and thus is not recommended. Also you have to
# set ApplicationSettings.auto_zooming to "system_dpi".
cef
.
Initialize
(
settings
=
settings
)
app
=
CefApp
(
False
)
app
.
MainLoop
()
del
app
# Must destroy before calling Shutdown
if
not
MAC
:
# On Mac shutdown is called in OnClose
cef
.
Shutdown
()
def
check_versions
():
print
(
"[wxpython.py] CEF Python {ver}"
.
format
(
ver
=
cef
.
__version__
))
print
(
"[wxpython.py] Python {ver} {arch}"
.
format
(
ver
=
platform
.
python_version
(),
arch
=
platform
.
architecture
()[
0
]))
print
(
"[wxpython.py] wxPython {ver}"
.
format
(
ver
=
wx
.
version
()))
# CEF Python version requirement
assert
cef
.
__version__
>=
"49.0"
,
"CEF Python v49.0+ required to run this"
class
MainFrame
(
wx
.
Frame
):
def
__init__
(
self
):
wx
.
Frame
.
__init__
(
self
,
parent
=
None
,
id
=
wx
.
ID_ANY
,
title
=
'wxPython example'
,
size
=
(
WIDTH
,
HEIGHT
))
self
.
browser
=
None
# Must ignore X11 errors like 'BadWindow' and others by
# installing X11 error handlers. This must be done after
# wx was intialized.
if
LINUX
:
WindowUtils
.
InstallX11ErrorHandlers
()
global
g_count_windows
g_count_windows
+=
1
self
.
setup_icon
()
self
.
create_menu
()
self
.
Bind
(
wx
.
EVT_CLOSE
,
self
.
OnClose
)
# Set wx.WANTS_CHARS style for the keyboard to work.
# This style also needs to be set for all parent controls.
self
.
browser_panel
=
wx
.
Panel
(
self
,
style
=
wx
.
WANTS_CHARS
)
self
.
browser_panel
.
Bind
(
wx
.
EVT_SET_FOCUS
,
self
.
OnSetFocus
)
self
.
browser_panel
.
Bind
(
wx
.
EVT_SIZE
,
self
.
OnSize
)
# On Linux must show before embedding browser, so that handle
# is available (Issue #347).
if
LINUX
:
self
.
Show
()
# In wxPython 3.0 and wxPython 4.0 handle is still
# not yet available, must delay embedding browser
# (Issue #349).
if
wx
.
version
().
startswith
(
"3."
)
or
wx
.
version
().
startswith
(
"4."
):
wx
.
CallLater
(
20
,
self
.
embed_browser
)
else
:
# This works fine in wxPython 2.8
self
.
embed_browser
()
else
:
self
.
embed_browser
()
self
.
Show
()
def
setup_icon
(
self
):
icon_file
=
os
.
path
.
join
(
os
.
path
.
abspath
(
os
.
path
.
dirname
(
__file__
)),
"resources"
,
"wxpython.png"
)
# wx.IconFromBitmap is not available on Linux in wxPython 3.0/4.0
if
os
.
path
.
exists
(
icon_file
)
and
hasattr
(
wx
,
"IconFromBitmap"
):
icon
=
wx
.
IconFromBitmap
(
wx
.
Bitmap
(
icon_file
,
wx
.
BITMAP_TYPE_PNG
))
self
.
SetIcon
(
icon
)
def
create_menu
(
self
):
filemenu
=
wx
.
Menu
()
filemenu
.
Append
(
1
,
"Some option"
)
filemenu
.
Append
(
2
,
"Another option"
)
menubar
=
wx
.
MenuBar
()
menubar
.
Append
(
filemenu
,
"&File"
)
self
.
SetMenuBar
(
menubar
)
def
embed_browser
(
self
):
window_info
=
cef
.
WindowInfo
()
(
width
,
height
)
=
self
.
browser_panel
.
GetClientSize
().
Get
()
window_info
.
SetAsChild
(
self
.
browser_panel
.
GetHandle
(),
[
0
,
0
,
width
,
height
])
self
.
browser
=
cef
.
CreateBrowserSync
(
window_info
,
url
=
"https://www.google.com/"
)
self
.
browser
.
SetClientHandler
(
FocusHandler
())
def
OnSetFocus
(
self
,
_
):
if
not
self
.
browser
:
return
if
WINDOWS
:
WindowUtils
.
OnSetFocus
(
self
.
browser_panel
.
GetHandle
(),
0
,
0
,
0
)
self
.
browser
.
SetFocus
(
True
)
def
OnSize
(
self
,
_
):
if
not
self
.
browser
:
return
if
WINDOWS
:
WindowUtils
.
UpdateBrowserSize
(
self
.
browser_panel
.
GetHandle
(),
self
.
browser
)
elif
LINUX
:
(
x
,
y
)
=
(
0
,
0
)
(
width
,
height
)
=
self
.
browser_panel
.
GetSize
().
Get
()
self
.
browser
.
SetBounds
(
x
,
y
,
width
,
height
)
self
.
browser
.
NotifyMoveOrResizeStarted
()
def
OnClose
(
self
,
event
):
print
(
"[wxpython.py] OnClose called"
)
if
not
self
.
browser
:
# May already be closing, may be called multiple times on Mac
return
if
MAC
:
# On Mac things work differently, other steps are required
self
.
browser
.
CloseBrowser
()
self
.
clear_browser_references
()
self
.
Destroy
()
global
g_count_windows
g_count_windows
-=
1
if
g_count_windows
==
0
:
cef
.
Shutdown
()
wx
.
GetApp
().
ExitMainLoop
()
# Call _exit otherwise app exits with code 255 (Issue #162).
# noinspection PyProtectedMember
os
.
_exit
(
0
)
else
:
# Calling browser.CloseBrowser() and/or self.Destroy()
# in OnClose may cause app crash on some paltforms in
# some use cases, details in Issue #107.
self
.
browser
.
ParentWindowWillClose
()
event
.
Skip
()
self
.
clear_browser_references
()
def
clear_browser_references
(
self
):
# Clear browser references that you keep anywhere in your
# code. All references must be cleared for CEF to shutdown cleanly.
self
.
browser
=
None
class
FocusHandler
(
object
):
def
OnGotFocus
(
self
,
browser
,
**
_
):
# Temporary fix for focus issues on Linux (Issue #284).
if
LINUX
:
print
(
"[wxpython.py] FocusHandler.OnGotFocus:"
" keyboard focus fix (Issue #284)"
)
browser
.
SetFocus
(
True
)
class
CefApp
(
wx
.
App
):
def
__init__
(
self
,
redirect
):
self
.
timer
=
None
self
.
timer_id
=
1
self
.
is_initialized
=
False
super
(
CefApp
,
self
).
__init__
(
redirect
=
redirect
)
def
OnPreInit
(
self
):
super
(
CefApp
,
self
).
OnPreInit
()
# On Mac with wxPython 4.0 the OnInit() event never gets
# called. Doing wx window creation in OnPreInit() seems to
# resolve the problem (Issue #350).
if
MAC
and
wx
.
version
().
startswith
(
"4."
):
print
(
"[wxpython.py] OnPreInit: initialize here"
" (wxPython 4.0 fix)"
)
self
.
initialize
()
def
OnInit
(
self
):
self
.
initialize
()
return
True
def
initialize
(
self
):
if
self
.
is_initialized
:
return
self
.
is_initialized
=
True
self
.
create_timer
()
frame
=
MainFrame
()
self
.
SetTopWindow
(
frame
)
frame
.
Show
()
def
create_timer
(
self
):
# See also "Making a render loop":
# http://wiki.wxwidgets.org/Making_a_render_loop
# Another way would be to use EVT_IDLE in MainFrame.
self
.
timer
=
wx
.
Timer
(
self
,
self
.
timer_id
)
self
.
Bind
(
wx
.
EVT_TIMER
,
self
.
on_timer
,
self
.
timer
)
self
.
timer
.
Start
(
10
)
# 10ms timer
def
on_timer
(
self
,
_
):
cef
.
MessageLoopWork
()
def
OnExit
(
self
):
self
.
timer
.
Stop
()
return
0
if
__name__
==
'__main__'
:
main
()
You can’t perform that action at this time.