因此,您的主要代码存在 2 个核心问题:
- 你没有初始化
cefpython
- 您
browser_frame
的配置不适合整个区域
简而言之,答案是:
import tkinter as tk
from tkinter import *
from cefpython3 import cefpython as cef
import ctypes
def main():
win = Tk()
cef.Initialize()
win.minsize(600,600)
win.grid_columnconfigure(0, weight=1)
win.grid_rowconfigure(0, weight=1)
#Create Frame
frame = Frame(win, bg='black')
frame.grid(row=0, column=0, sticky=('NSWE'))
# Create Browser Frame
browser_frame = BrowserFrame(frame)
browser_frame.pack(fill=tk.BOTH, expand=tk.YES)
win.mainloop()
cef.Shutdown()
class BrowserFrame(tk.Frame):
def __init__(self, mainframe, navigation_bar=None):
self.navigation_bar = navigation_bar
self.closing = False
self.browser = None
tk.Frame.__init__(self, mainframe)
self.mainframe = mainframe
self.bind("<FocusIn>", self.on_focus_in)
self.bind("<FocusOut>", self.on_focus_out)
self.bind("<Configure>", self.on_configure)
"""For focus problems see Issue #255 and Issue #535. """
self.focus_set()
#URLURLURL
def embed_browser(self):
window_info = cef.WindowInfo()
rect = [0, 0, self.winfo_width(), self.winfo_height()]
window_info.SetAsChild(self.get_window_handle(), rect)
self.browser = cef.CreateBrowserSync(window_info,
url="file:///calculo.html")
assert self.browser
#self.browser.SetClientHandler(LifespanHandler(self))
#self.browser.SetClientHandler(LoadHandler(self))
#self.browser.SetClientHandler(FocusHandler(self))
self.message_loop_work()
def get_window_handle(self):
if self.winfo_id() > 0:
return self.winfo_id()
else:
raise Exception("Couldn't obtain window handle")
def message_loop_work(self):
cef.MessageLoopWork()
self.after(10, self.message_loop_work)
def on_configure(self, _):
if not self.browser:
self.embed_browser()
def on_root_configure(self):
# Root <Configure> event will be called when top window is moved
if self.browser:
self.browser.NotifyMoveOrResizeStarted()
def on_mainframe_configure(self, width, height):
if self.browser:
if WINDOWS:
ctypes.windll.user32.SetWindowPos(
self.browser.GetWindowHandle(), 0,
0, 0, width, height, 0x0002)
self.browser.NotifyMoveOrResizeStarted()
def on_focus_in(self, _):
#logger.debug("BrowserFrame.on_focus_in")
if self.browser:
self.browser.SetFocus(True)
def on_focus_out(self, _):
#logger.debug("BrowserFrame.on_focus_out")
"""For focus problems see Issue #255 and Issue #535. """
pass
def on_root_close(self):
#logger.info("BrowserFrame.on_root_close")
if self.browser:
#logger.debug("CloseBrowser")
self.browser.CloseBrowser(True)
self.clear_browser_references()
else:
#logger.debug("tk.Frame.destroy")
self.destroy()
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 LifespanHandler(object):
def __init__(self, tkFrame):
self.tkFrame = tkFrame
def OnBeforeClose(self, browser, **_):
#logger.debug("LifespanHandler.OnBeforeClose")
self.tkFrame.quit()
class LoadHandler(object):
def __init__(self, browser_frame):
self.browser_frame = browser_frame
def OnLoadStart(self, browser, **_):
if self.browser_frame.master.navigation_bar:
self.browser_frame.master.navigation_bar.set_url(browser.GetUrl())
class FocusHandler(object):
"""For focus problems see Issue #255 and Issue #535. """
def __init__(self, browser_frame):
self.browser_frame = browser_frame
def OnTakeFocus(self, next_component, **_):
pass#logger.debug("FocusHandler.OnTakeFocus, next={next}".format(next=next_component))
def OnSetFocus(self, source, **_):
return True
def OnGotFocus(self, **_):
#logger.debug("FocusHandler.OnGotFocus")
pass
if __name__ == '__main__':
main()
因此,要解决问题№ 1,您必须在 tkinter 主循环中键入cef.Initialize()
,然后再将cef.Shutdown()
其关闭。问题是我们仍然看不到框架,因为你没有这样做,所以即使你做了它grid_columnconfigure
也没有做任何事情,因为行和列没有添加任何重量,为了纠正这个问题,我使用了 pack。grid_rowconfigure
sticky='nsew'
在为我创建的 Windows 上:
令人讨厌的是,由于未加载 MathJax,所以这不能很好地工作cefpython
,要纠正这个问题,您需要更改您calculo.html
的阅读:
<!DOCTYPE html>
<html>
<head>
<title>MathJax TeX Test Page</title>
<script type="text/x-mathjax-config" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
</head>
<body>
When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$ \[\\ yep \\x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]
</body>
</html>
因为内联类型text/x-mathjax-config
不受cefpython
.
这将创建:
就像快速指针一样,这确实需要几秒钟才能加载 mathjax,因此托管本地 MathJax 配置可能是一个想法,因为这很可能会加快加载时间。
关于在此之上有一个标签,通过简单地将变量 frame 移动到grid(row=1, column=0...
(并更改行权重)然后在grid(row=0, column=0...
.