python:tkinter + cef 模仿 mdict 界面

news/2025/1/8 21:58:48/

cefpython3 其上游是C++开发的CEF(基于webkit、V8),
CEF 即 (Chromium Embedder Framework),
是基于Google Chromium项目的开源 Web browser控件(WebView)。

pip install cefpython3
cefpython3-66.1-py2.py3-none-win_amd64.whl (69.0 MB)
Successfully installed cefpython3-66.1

cd \Python37\Lib\site-packages\cefpython3\examples
copy tkinter_.py  tk_cef.py
用的图片在 \Python37\Lib\site-packages\cefpython3\examples\resources\
编写 tk_cef.py 如下

# -*- coding: utf-8 -*-
# Example of embedding CEF Python browser using Tkinter toolkit.
# This example has two widgets: a navigation bar and a browser.
#
# Tested configurations:
# - Tk 8.5 on Windows/Mac
# - Tk 8.6 on Linux
# - CEF Python v55.3+
#
import ctypes
try:import tkinter as tk
except ImportError:import Tkinter as tk
import sys
import os
import platform
import logging as _logging
from cefpython3 import cefpython as cef# Fix for PyCharm hints warnings
WindowUtils = cef.WindowUtils()# Platforms
WINDOWS = (platform.system() == "Windows")
LINUX = (platform.system() == "Linux")
MAC = (platform.system() == "Darwin")# Globals
logger = _logging.getLogger("tk_cef.py")
baseurl = "http://localhost:8888/"# Constants
# Tk 8.5 doesn't support png images
IMAGE_EXT = ".png" if tk.TkVersion > 8.5 else ".gif"def main():logger.setLevel(_logging.DEBUG)stream_handler = _logging.StreamHandler()formatter = _logging.Formatter("[%(filename)s] %(message)s")stream_handler.setFormatter(formatter)logger.addHandler(stream_handler)logger.info("CEF Python {ver}".format(ver=cef.__version__))logger.info("Python {ver} {arch}".format(ver=platform.python_version(), arch=platform.architecture()[0]))logger.info("Tk {ver}".format(ver=tk.Tcl().eval('info patchlevel')))assert cef.__version__ >= "55.3", "CEF Python v55.3+ required to run this"# 替换python预定义异常处理逻辑,为保证异常发生时能够结束所有进程sys.excepthook = cef.ExceptHook  # To shutdown all CEF processes on error# Tk must be initialized before CEF otherwise fatal error (Issue #306)root = tk.Tk()app = MainFrame(root)settings = {}if MAC:settings["external_message_pump"] = Truecef.Initialize(settings=settings)app.mainloop()logger.debug("Main loop exited")cef.Shutdown() # 结束进程class MainFrame(tk.Frame):""" tk GUI 主界面 """def __init__(self, root):self.browser_frame = Noneself.navigation_bar = Noneself.root = root# Rootroot.geometry("900x640")tk.Grid.rowconfigure(root, 0, weight=1)tk.Grid.columnconfigure(root, 0, weight=1)# MainFrametk.Frame.__init__(self, root)self.master.title("tkinter + cef")self.master.protocol("WM_DELETE_WINDOW", self.on_close)self.master.bind("<Configure>", self.on_root_configure)self.setup_icon()self.bind("<Configure>", self.on_configure)self.bind("<FocusIn>", self.on_focus_in)self.bind("<FocusOut>", self.on_focus_out)# NavigationBarself.navigation_bar = NavigationBar(self)self.navigation_bar.grid(row=0, column=0,sticky=(tk.N + tk.S + tk.E + tk.W))tk.Grid.rowconfigure(self, 0, weight=0)tk.Grid.columnconfigure(self, 0, weight=0)# BrowserFrameself.browser_frame = BrowserFrame(self, self.navigation_bar)self.browser_frame.grid(row=1, column=0,sticky=(tk.N + tk.S + tk.E + tk.W))tk.Grid.rowconfigure(self, 1, weight=1)tk.Grid.columnconfigure(self, 0, weight=1)# Pack MainFrameself.pack(fill=tk.BOTH, expand=tk.YES)def on_root_configure(self, _):#logger.debug("MainFrame.on_root_configure")if self.browser_frame:self.browser_frame.on_root_configure()def on_configure(self, event):logger.debug("MainFrame.on_configure")if self.browser_frame:width = event.widthheight = event.heightif self.navigation_bar:height = height - self.navigation_bar.winfo_height()self.browser_frame.on_mainframe_configure(width, height)def on_focus_in(self, _):logger.debug("MainFrame.on_focus_in")def on_focus_out(self, _):logger.debug("MainFrame.on_focus_out")def on_close(self):if self.browser_frame:self.browser_frame.on_root_close()self.browser_frame = Noneelse:self.master.destroy()def get_browser(self):if self.browser_frame:return self.browser_frame.browserreturn Nonedef get_browser_frame(self):if self.browser_frame:return self.browser_framereturn Nonedef setup_icon(self):resources = os.path.join(os.path.dirname(__file__), "resources")icon_path = os.path.join(resources, "tkinter"+IMAGE_EXT)if os.path.exists(icon_path):self.icon = tk.PhotoImage(file=icon_path)# noinspection PyProtectedMemberself.master.call("wm", "iconphoto", self.master._w, self.icon)class BrowserFrame(tk.Frame):def __init__(self, mainframe, navigation_bar=None):self.navigation_bar = navigation_barself.closing = Falseself.browser = Nonetk.Frame.__init__(self, mainframe)self.mainframe = mainframeself.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()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)global baseurl # 创建浏览器self.browser = cef.CreateBrowserSync(window_info, url=baseurl)assert self.browserself.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 movedif 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)elif LINUX:self.browser.SetBounds(0, 0, width, height)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. """if LINUX and self.browser:self.browser.SetFocus(False)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 = Noneclass LifespanHandler(object):def __init__(self, tkFrame):self.tkFrame = tkFramedef OnBeforeClose(self, browser, **_):logger.debug("LifespanHandler.OnBeforeClose")self.tkFrame.quit()class LoadHandler(object):def __init__(self, browser_frame):self.browser_frame = browser_framedef OnLoadStart(self, browser, **_):pass#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_framedef OnTakeFocus(self, next_component, **_):logger.debug("FocusHandler.OnTakeFocus, next={next}".format(next=next_component))def OnSetFocus(self, source, **_):logger.debug("FocusHandler.OnSetFocus, source={source}".format(source=source))if LINUX:return Falseelse:return Truedef OnGotFocus(self, **_):logger.debug("FocusHandler.OnGotFocus")if LINUX:self.browser_frame.focus_set()class NavigationBar(tk.Frame):def __init__(self, master):self.back_state = tk.NONEself.forward_state = tk.NONEself.back_image = Noneself.forward_image = Noneself.reload_image = Nonetk.Frame.__init__(self, master)resources = os.path.join(os.path.dirname(__file__), "resources")# Back buttonback_png = os.path.join(resources, "back"+IMAGE_EXT)if os.path.exists(back_png):self.back_image = tk.PhotoImage(file=back_png)self.back_button = tk.Button(self, image=self.back_image,command=self.go_back)self.back_button.grid(row=0, column=0, padx=5, pady=2)# Forward buttonforward_png = os.path.join(resources, "forward"+IMAGE_EXT)if os.path.exists(forward_png):self.forward_image = tk.PhotoImage(file=forward_png)self.forward_button = tk.Button(self, image=self.forward_image,command=self.go_forward)self.forward_button.grid(row=0, column=1, padx=5, pady=2)# Reload buttonreload_png = os.path.join(resources, "reload"+IMAGE_EXT)if os.path.exists(reload_png):self.reload_image = tk.PhotoImage(file=reload_png)self.reload_button = tk.Button(self, image=self.reload_image,command=self.reload)self.reload_button.grid(row=0, column=2, padx=5, pady=2)# Url entryself.url_entry = tk.Entry(self)self.url_entry.bind("<FocusIn>", self.on_url_focus_in)self.url_entry.bind("<FocusOut>", self.on_url_focus_out)self.url_entry.bind("<Return>", self.on_load_url)self.url_entry.bind("<Button-1>", self.on_button1)self.url_entry.grid(row=0, column=3,sticky=(tk.N + tk.S + tk.E + tk.W))tk.Grid.rowconfigure(self, 0, weight=100)tk.Grid.columnconfigure(self, 3, weight=100)# go buttonself.go_button = tk.Button(self, text="go", command=self.go)self.go_button.grid(row=0, column=4, padx=5, pady=2)# prefix button 前缀匹配self.button2 = tk.Button(self, text="prefix", command=self.prefix)self.button2.grid(row=0, column=5, padx=5, pady=2)# Update state of buttonsself.update_state()def go(self):global baseurlurl = self.url_entry.get()if url.strip() =='':returnelif url.startswith("http://"):passelse:url = baseurl + "trans?txt=" + url.strip();if self.master.get_browser():self.master.get_browser().StopLoad()self.master.get_browser().LoadUrl(url)def prefix(self):global baseurlurl = self.url_entry.get()if url.strip() =='':returnelif url.startswith("http://"):if self.master.get_browser():self.master.get_browser().StopLoad()self.master.get_browser().LoadUrl(url)else:url = baseurl + "prefix?txt=" + url.strip();if self.master.get_browser():self.master.get_browser().StopLoad()self.master.get_browser().LoadUrl(url)def go_back(self):if self.master.get_browser():self.master.get_browser().GoBack()def go_forward(self):if self.master.get_browser():self.master.get_browser().GoForward()def reload(self):if self.master.get_browser():self.master.get_browser().Reload()def set_url(self, url):self.url_entry.delete(0, tk.END)self.url_entry.insert(0, url)def on_url_focus_in(self, _):logger.debug("NavigationBar.on_url_focus_in")def on_url_focus_out(self, _):logger.debug("NavigationBar.on_url_focus_out")def on_load_url(self, _):if self.master.get_browser():self.master.get_browser().StopLoad()self.master.get_browser().LoadUrl(self.url_entry.get())def on_button1(self, _):"""For focus problems see Issue #255 and Issue #535. """logger.debug("NavigationBar.on_button1")self.master.master.focus_force()def update_state(self):browser = self.master.get_browser()if not browser:if self.back_state != tk.DISABLED:self.back_button.config(state=tk.DISABLED)self.back_state = tk.DISABLEDif self.forward_state != tk.DISABLED:self.forward_button.config(state=tk.DISABLED)self.forward_state = tk.DISABLEDself.after(100, self.update_state)returnif browser.CanGoBack():if self.back_state != tk.NORMAL:self.back_button.config(state=tk.NORMAL)self.back_state = tk.NORMALelse:if self.back_state != tk.DISABLED:self.back_button.config(state=tk.DISABLED)self.back_state = tk.DISABLEDif browser.CanGoForward():if self.forward_state != tk.NORMAL:self.forward_button.config(state=tk.NORMAL)self.forward_state = tk.NORMALelse:if self.forward_state != tk.DISABLED:self.forward_button.config(state=tk.DISABLED)self.forward_state = tk.DISABLEDself.after(100, self.update_state)if __name__ == '__main__':main()

这个程序解决了 tkinter 嵌入 cef 后鼠标焦点失控的问题。

web 服务程序:python:mdict + bottle = web 查询英汉词典


http://www.ppmy.cn/news/1043580.html

相关文章

神经网络基础-神经网络补充概念-62-池化层

概念 池化层&#xff08;Pooling Layer&#xff09;是深度学习神经网络中常用的一种层级结构&#xff0c;用于减小输入数据的空间尺寸&#xff0c;从而降低模型的计算复杂度&#xff0c;减少过拟合&#xff0c;并且在一定程度上提取输入数据的重要特征。池化层通常紧跟在卷积层…

Android 面试笔记整理-Binder机制

作者&#xff1a;浪人笔记 面试可能会问到的问题 从IPC的方式问到Binder的优势为什么zygote跟其他服务进程的通讯不使用BinderBinder线程池和Binder机制 等等这些问题都是基于你对Binder的理解还有对其他IPC通讯的理解 IPC方式有多少种 传统的IPC方式有Socket、共享内存、管道…

华纳云:搭建服务器镜像运行环境的步骤

搭建服务器镜像运行环境涉及多个步骤&#xff0c;包括选择操作系统、配置软件和服务、进行安全设置等。以下是一个基本的指南&#xff0c;可以帮助您开始搭建服务器镜像运行环境&#xff1a; 选择操作系统&#xff1a; 根据您的需求和偏好&#xff0c;选择适合的操作系统。常见…

java forEach中不能使用break和continue的原因

1.首先了解break和continue的使用范围和作用 1.1使用范围 break适用范围&#xff1a;只能用于switch或者是循环语句中。当然可以用于增强for循环。 continue适用范围: 用于循环语句中。 1.2作用 break: 1. break用于switch语句的作用是结束一个switch语句。 2. break用于循…

插入排序优化——超越归并排序的超级算法

插入排序及优化 插入排序算法算法讲解数据模拟代码 优化思路一、二分查找二、copy函数 优化后代码算法的用途题目&#xff1a;数星星&#xff08;POJ2352 star&#xff09;输入输出格式输入格式&#xff1a;输出格式 输入输出样例输入样例输出样例 题目讲解步骤如下AC 代码 插入…

[bug] 记录version `GLIBCXX_3.4.29‘ not found 解决方法

在使用mediapipe 这个库的时候&#xff0c;首次使用出现 GLIBCXX_3.4.29’ not found 错误&#xff0c; 看起来是安装mediapipe 的时候自动升级了 matplotlib 这个库&#xff0c;导致依赖的 libstd.so 版本不满足了&#xff0c;GLIBCXX_3.4.29 is an object from libstdc.so.…

【linux基础(四)】对Linux权限的理解

&#x1f493;博主CSDN主页:杭电码农-NEO&#x1f493;   ⏩专栏分类:Linux从入门到开通⏪   &#x1f69a;代码仓库:NEO的学习日记&#x1f69a;   &#x1f339;关注我&#x1faf5;带你学更多操作系统知识   &#x1f51d;&#x1f51d; Linux权限 1. 前言2. shell命…

微信小程序自定义tabBar详细教程,且自适应尺寸和实现高斯模糊版

IOS示例&#xff1a; 安卓示例&#xff1a; 高斯模糊示例&#xff1a; WXML代码&#xff1a; <view class"tabbar"><view style"{{selectindex&&index0?color: #ef3166;:}}"class"tabbar-item {{ select index ? tabbar-select …