Skip to content

Instantly share code, notes, and snippets.

@xupengzhuo
Created November 18, 2017 11:52
Show Gist options
  • Select an option

  • Save xupengzhuo/77449ad724a09fb0be363cd55884a48e to your computer and use it in GitHub Desktop.

Select an option

Save xupengzhuo/77449ad724a09fb0be363cd55884a48e to your computer and use it in GitHub Desktop.
pythonnet&cef&wpf
import clr
import sys
if sys.platform.lower() not in ['cli','win32']:
print("only windows is supported for wpf")
clr.AddReference(r"wpf\PresentationFramework")
from System.IO import StreamReader
from System.Windows.Markup import XamlReader
from System.Threading import Thread, ThreadStart, ApartmentState
from System.Windows import Application, Window, WindowState
from System.Windows.Interop import WindowInteropHelper
from System.Diagnostics import Process
from cefpython3 import cefpython as cef
import threading
import multiprocessing
class q(object):
def __init__(self):
print('hahaha')
class CefWindow(Window):
def __init__(self):
self.Title = "WPF From PythonNet"
self.Show()
self.KeyDown += self.ccc
self.MouseEnter += self.ccc
#self.Initialized += self.OnSourceInitialized
self.StateChanged += self.ccc
#self.KeyDown += self.bbb
self.bbb()
Application().Run()
def bbb(self,*args):
print('ccccc')
sys.excepthook = cef.ExceptHook
cef.Initialize({'multi_threaded_message_loop':False})
print(WindowInteropHelper(self).Handle)
window_info = cef.WindowInfo()
print(window_info)
window_info.SetAsChild(WindowInteropHelper(self).Handle.ToInt64())
cef.CreateBrowserSync(url='https://www.baidu.com', window_info=window_info)
print('???',Process.GetCurrentProcess().Id)
print(self.aaa)
self.xxx()
q()
print('...',threading.current_thread())
print(threading.main_thread())
cef.MessageLoop()
def OnSourceInitialized(self, *args):
self.WindowState = WindowState.Maximized
print(11111, *args)
def ccc(self, *args):
print(666,args)
@classmethod
def xxx(cls):
cls.aaa='bbbbbbb'
#if __name__ == '__main__':
CefWindow.aaa = 'out'
thread = Thread(ThreadStart(CefWindow))
thread.SetApartmentState(ApartmentState.STA)
print(thread.ManagedThreadId)
thread.Start()
print('--->',threading.active_count())
print(multiprocessing.current_process().pid)
print('vvvv',threading.current_thread())
import time
while 'In loop':
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment