python tkinter mainloop_为什么不能同时运行tkinter mainloop和cefpython3 messageloop?

I'm working at a project in Python3 in which i have both tkinter and a frame in tkinter with cef browser.

This is the code.

from tkinter import messagebox

#import threading

from cefpython3 import cefpython as cef

import platform

import sys

from tkinter import *

import time

def on_closing ():

print('closing')

r.destroy()

cef.Shutdown()

r = Tk()

r.geometry('800x600')

r.protocol('WM_DELETE_WINDOW', on_closing)

f = Frame(r, bg = 'blue', height = 200)

f.pack(side = TOP, fill = 'x')

g=Frame(r,bg = 'white',height = 200)

g.pack(side = TOP, fill = 'x')

b1 = Button (g,text='Exit',command = on_closing)

b1.pack (side = LEFT)

b2 = Button (g,text='Show something',command = lambda:messagebox.showinfo('TITLE', 'Shown something'))

b2.pack (side = RIGHT)

sys.excepthook = cef.ExceptHook

rect = [0, 0, 800, 200]

print('browser: ', rect[2],'x',rect[3])

window_info=cef.WindowInfo(f.winfo_id())

window_info.SetAsChild(f.winfo_id(),rect)

cef.Initialize()

browser = cef.CreateBrowserSync(window_info, url='http://www.google.com')

r.update()

cef.MessageLoop()

##_thread = threading.Thread(target=cef.MessageLoop)

##

##_thread.start()

##

##_thread.join()

r.mainloop()

print('end')

The problem is:

I leave cef.MessageLoop() and the browser works but buttons don't.

I comment out cef.MessageLoop() and the browser doesn't work but

tkinter window does.

I was thinking that maybe threading module wuold help but i tried (you can see the commented lines) and doesn't work (i get no exceptions but browser don't work).

How can i sort this out?

解决方案

Tkinter runs in a single thread so when you write what is basically an infinite loop inside of it then you will block Tkinter from working. The only reason you screen is coming up at all is because you used update() but that will not fix the issue here.

The solution will be to use threading to manage the MessageLoop in a separate thread while also passing the frame to the function to allow for some interaction between Tkinter and cef.

Note: I also cleaned up your code a bit to better follow PEP8 standards.

import tkinter as tk

from tkinter import messagebox

from cefpython3 import cefpython as cef

import threading

import sys

def test_thread(frame):

sys.excepthook = cef.ExceptHook

window_info = cef.WindowInfo(frame.winfo_id())

window_info.SetAsChild(frame.winfo_id(), rect)

cef.Initialize()

browser = cef.CreateBrowserSync(window_info, url='http://www.google.com')

cef.MessageLoop()

def on_closing():

print('closing')

root.destroy()

root = tk.Tk()

root.geometry('800x600')

root.protocol('WM_DELETE_WINDOW', on_closing)

frame = tk.Frame(root, bg='blue', height=200)

frame2 = tk.Frame(root, bg='white', height=200)

frame.pack(side='top', fill='x')

frame2.pack(side='top', fill='x')

tk.Button(frame2, text='Exit', command=on_closing).pack(side='left')

tk.Button(frame2, text='Show something',

command=lambda: messagebox.showinfo('TITLE', 'Shown something')).pack(side='right')

rect = [0, 0, 800, 200]

print('browser: ', rect[2], 'x', rect[3])

thread = threading.Thread(target=test_thread, args=(frame,))

thread.start()

root.mainloop()

Results:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值