python cef,wxPython和CEF Python 3

这篇博客介绍了如何在Windows环境下使用CEFpython3时避免在命令提示符中显示调试窗口。示例代码展示了初始化调试、异常处理和日志记录的实现,但并未直接提供关闭调试窗口的解决方案。DEBUG=True仅停止内部调试,窗口仍会显示。作者寻求关闭调试窗口的方法。
摘要由CSDN通过智能技术生成

查看Windows上CEF python3中的示例文件。在

运行Python示例脚本时,它会在Windows命令提示符下打开一个调试窗口。在

我想知道怎么才能不显示这个?在

我想说的是:

DEBUG=True——没有什么区别,它只是停止该窗口内的调试,但窗口仍然显示。

这是示例.py文件:# CEF Python 3 example application.

# Checking whether python architecture and version are valid, otherwise an obfuscated

# error will be thrown when trying to load cefpython.pyd with a message "DLL load failed".

import platform

if platform.architecture()[0] != "32bit":

raise Exception("Architecture not supported: %s" % platform.architecture()[0])

import os, sys

libcef_dll = os.path.join(os.path.dirname(os.path.abspath(__file__)),

'libcef.dll')

if os.path.exists(libcef_dll):

# Import the local module.

if 0x02070000 <= sys.hexversion < 0x03000000:

import cefpython_py27 as cefpython

elif 0x03000000 <= sys.hexversion < 0x04000000:

import cefpython_py32 as cefpython

else:

raise Exception("Unsupported python version: %s" % sys.version)

else:

# Import the package.

from cefpython3 import cefpython

import cefwindow

import win32con

import win32gui

import time

DEBUG = True

def GetApplicationPath(file=None):

import re, os

# If file is None return current directory without trailing slash.

if file is None:

file = ""

# Only when relative path.

if not file.startswith("/") and not file.startswith("\\") and (

not re.search(r"^[\w-]+:", file)):

if hasattr(sys, "frozen"):

path = os.path.dirname(sys.executable)

elif "__file__" in globals():

path = os.path.dirname(os.path.realpath(__file__))

else:

path = os.getcwd()

path = path + os.sep + file

path = re.sub(r"[/\\]+", re.escape(os.sep), path)

path = re.sub(r"[/\\]+$", "", path)

return path

return str(file)

def ExceptHook(excType, excValue, traceObject):

import traceback, os, time, codecs

# This hook does the following: in case of exception write it to

# the "error.log" file, display it to the console, shutdown CEF

# and exit application immediately by ignoring "finally" (_exit()).

errorMsg = "\n".join(traceback.format_exception(excType, excValue,

traceObject))

errorFile = GetApplicationPath("error.log")

try:

appEncoding = cefpython.g_applicationSettings["string_encoding"]

except:

appEncoding = "utf-8"

if type(errorMsg) == bytes:

errorMsg = errorMsg.decode(encoding=appEncoding, errors="replace")

try:

with codecs.open(errorFile, mode="a", encoding=appEncoding) as fp:

fp.write("\n[%s] %s\n" % (

time.strftime("%Y-%m-%d %H:%M:%S"), errorMsg))

except:

print("cefpython: WARNING: failed writing to error file: %s" % (

errorFile))

# Convert error message to ascii before printing, otherwise

# you may get error like this:

# | UnicodeEncodeError: 'charmap' codec can't encode characters

errorMsg = errorMsg.encode("ascii", errors="replace")

errorMsg = errorMsg.decode("ascii", errors="replace")

print("\n"+errorMsg+"\n")

cefpython.QuitMessageLoop()

cefpython.Shutdown()

os._exit(1)

def InitDebugging():

# Whether to print & log debug messages

if DEBUG:

cefpython.g_debug = True

cefpython.g_debugFile = GetApplicationPath("debug.log")

cefwindow.g_debug = True

def CefAdvanced():

sys.excepthook = ExceptHook

InitDebugging()

appSettings = dict()

appSettings["log_file"] = GetApplicationPath("debug.log")

appSettings["log_severity"] = cefpython.LOGSEVERITY_INFO

appSettings["release_dcheck_enabled"] = True # Enable only when debugging

appSettings["browser_subprocess_path"] = "%s/%s" % (

cefpython.GetModuleDirectory(), "subprocess")

cefpython.Initialize(appSettings)

wndproc = {

win32con.WM_CLOSE: CloseWindow,

win32con.WM_DESTROY: QuitApplication,

win32con.WM_SIZE: cefpython.WindowUtils.OnSize,

win32con.WM_SETFOCUS: cefpython.WindowUtils.OnSetFocus,

win32con.WM_ERASEBKGND: cefpython.WindowUtils.OnEraseBackground

}

browserSettings = dict()

browserSettings["universal_access_from_file_urls_allowed"] = True

browserSettings["file_access_from_file_urls_allowed"] = True

windowHandle = cefwindow.CreateWindow(title="CEF Python 3 example",

className="cefpython3_example", width=800, height=600,

icon="icon.ico", windowProc=wndproc)

windowInfo = cefpython.WindowInfo()

windowInfo.SetAsChild(windowHandle)

browser = cefpython.CreateBrowserSync(windowInfo, browserSettings,

navigateUrl=GetApplicationPath("example.html"))

cefpython.MessageLoop()

cefpython.Shutdown()

def CloseWindow(windowHandle, message, wparam, lparam):

browser = cefpython.GetBrowserByWindowHandle(windowHandle)

browser.CloseBrowser()

return win32gui.DefWindowProc(windowHandle, message, wparam, lparam)

def QuitApplication(windowHandle, message, wparam, lparam):

win32gui.PostQuitMessage(0)

return 0

if __name__ == "__main__":

CefAdvanced()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值