0

谁能帮我把这个 browser_frame 设置在正确的地方?

它指向正确的位置,但它没有在正确的位置打开

import ctypes
import platform
from cefpython3 import cefpython as cef
from tkinter import *
import tkinter as tk
import sys
from cefpython.examples.tkinter_ import logger


WINDOWS = platform.system() == 'Windows'
LINUX = platform.system() == 'Linux'
MAC = platform.system() == 'Darwin'

class BrowserFrame(tk.LabelFrame):
    def __init__(self, master=None, **kw):
        super().__init__(master, text='Browser', **kw)
        self.browser = None
        self.bind('<Configure>', self.on_configure)

    def winfo_id(self):
        return super.winfo_id()

    def get_window_handle(self):
        if MAC:
            from AppKit import NSApp
            import objc
            return objc.pyobjc_id(NSApp.windows()[-1].contentView())
        elif self.winfo_id() > 0:
            return self.winfo_id()
        else:
            raise Exception('Could not obtain window handle!')

    def cef_loop(self):
        cef.MessageLoopWork()
        self.after(10, self.cef_loop)


    def on_configure(self, event):
        if self.browser is None:
            # create the browser and embed it in current frame
            self.update()
            rect = [0, 0, 500, 450]
            cef_winfo = cef.WindowInfo()
            win_id = self.get_window_handle()
            cef_winfo.SetAsChild(win_id, rect)
            self.browser = cef.CreateBrowserSync(cef_winfo, url=urlset)
            
            
            print("self: ",self)
            print("rect: ",rect)
            print("win_id: ",win_id)
            print("self.winfo_width(), self.winfo_height(): ",self.winfo_width(), self.winfo_height())
            print("self.browser: ",self.browser)
            print("cef_winfo: ",cef_winfo)

            # start the browser handling loop
            self.cef_loop()

        # resize the browser
        if WINDOWS:
            ctypes.windll.user32.SetWindowPos(
                self.browser.GetWindowHandle(), 0,
                0, 0, event.width, event.height, 0x0002)
        elif LINUX:
            self.browser.SetBounds(0, 0, event.width, event.height)




def main():
    root = tk.Tk()
    root.columnconfigure(0, weight=1)
    root.columnconfigure(1, weight=2)
    root.rowconfigure(0, weight=1)
    w, h = root.winfo_screenwidth(), root.winfo_screenheight()
    root.geometry("%dx%d+0+0" % (w-250, h-300))
    root.title('Test')


    WindowUtils = cef.WindowUtils()
    sys.excepthook = cef.ExceptHook  # To shutdown all CEF processes on error
    settings = {}
    if MAC:
        settings["external_message_pump"] = True

    cef.Initialize(settings=settings)


    leftfrm = tk.LabelFrame(root, text="Left", padx=5, pady=5, bg='red')
    leftfrm.grid(row=0, column=0, sticky='news', pady=2)

    home_browser = tk.LabelFrame(root, text="home_of_the_browser", padx=5, pady=5, bg='blue')
    home_browser.grid(row=0, column=1, pady=2,ipady=200,ipadx=400)
    home_browser.columnconfigure(1, weight=1)
    home_browser.rowconfigure(0, weight=1)


    browser_frame = BrowserFrame(home_browser, bg='green')
    browser_frame.grid(row=0, column=1, sticky=('new'))
    browser_frame.columnconfigure(1, weight=1)
    browser_frame.rowconfigure(0, weight=1)

    print("->", leftfrm) #labelframe
    print("->", home_browser) #labelframe2
    print("->", browser_frame) #!labelframe2.!browserframe

    for x in range(1, 25):
        tk.Label(leftfrm, text=f"Link {x}", bg='yellow').grid(row=x, column=0)

    global urlset
    urlset = "http://www.google.com"




    root.mainloop()


if __name__ == '__main__':
    main()

-> .!labelframe

-> .!labelframe2

-> .!labelframe2.!browserframe

自我:.!labelframe2.!browserframe

矩形:[0, 0, 500, 450]

win_id: 140457658132352

self.winfo_width(), self.winfo_height(): 938 1

self.browser: <cefpython_py39.PyBrowser object at 0x7fbedfba53c0>

cef_winfo: <cefpython_py39.WindowInfo 对象在 0x7fbedd9f0310>

在此处输入图像描述

4

0 回答 0