|
| 1 | + |
| 2 | +# -- coding=utf8 -- |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import platform |
| 6 | +import tkinter |
| 7 | +import tkinter.messagebox |
| 8 | +import tkinter.ttk |
| 9 | +import ctypes |
| 10 | +import json |
| 11 | +import socket |
| 12 | +from cefpython3 import cefpython as cef |
| 13 | +ThisDialog = None |
| 14 | +TargetURL = "http://www.py-me.com" |
| 15 | +if len(sys.argv) > 1: |
| 16 | + TargetURL = sys.argv[1] |
| 17 | +WINDOWS = (platform.system() == "Windows") |
| 18 | +#用于嵌入浏览器 |
| 19 | +class Visitor(object): |
| 20 | + def SetCallBack(self,callback): |
| 21 | + self.callback = callback |
| 22 | + |
| 23 | + def Visit(self, value): |
| 24 | + #print(value) |
| 25 | + if self.callback: |
| 26 | + self.callback(value) |
| 27 | +#Frame中的浏览器 |
| 28 | +class BrowserFrame(tkinter.ttk.Frame): |
| 29 | + def __init__(self, master, navigation_bar=None): |
| 30 | + try: |
| 31 | + cef.Initialize() |
| 32 | + self.navigation_bar = navigation_bar |
| 33 | + self.closing = False |
| 34 | + self.browser = None |
| 35 | + self.URL = "http://www.py-me.com" |
| 36 | + self.stringVisitor = Visitor() |
| 37 | + tkinter.ttk.Frame.__init__(self, master) |
| 38 | + self.bind("<Configure>", self.on_configure) |
| 39 | + print("BrowserFrame") |
| 40 | + except Exception as ex: |
| 41 | + tkinter.messagebox.showwarning("__init__",str(ex)) |
| 42 | + def setURL(self,url): |
| 43 | + self.URL = url |
| 44 | + try: |
| 45 | + if self.browser: |
| 46 | + self.browser.LoadUrl(self.URL) |
| 47 | + print("setURL:"+self.URL) |
| 48 | + except Exception as ex: |
| 49 | + tkinter.messagebox.showwarning("setURL",str(ex)) |
| 50 | + def getSourceCode(self): |
| 51 | + if self.browser: |
| 52 | + try: |
| 53 | + #获取源码 |
| 54 | + mainFrame = self.browser.GetMainFrame() |
| 55 | + winhandle = self.get_window_handle() |
| 56 | + if mainFrame and winhandle: |
| 57 | + mainFrame.GetSource(self.stringVisitor) |
| 58 | + except Exception as ex: |
| 59 | + tkinter.messagebox.showwarning("getSourceCode",str(ex)) |
| 60 | + return self.stringVisitor |
| 61 | + def embed_browser(self,width,heith): |
| 62 | + try: |
| 63 | + window_info = cef.WindowInfo() |
| 64 | + rect = [0, 0, width, heith] |
| 65 | + window_info.SetAsChild(self.get_window_handle(), rect) |
| 66 | + self.browser = cef.CreateBrowserSync(window_info,url=self.URL) |
| 67 | + assert self.browser |
| 68 | + self.message_loop_work() |
| 69 | + except Exception as ex: |
| 70 | + tkinter.messagebox.showwarning("embed_browser",str(ex)) |
| 71 | + |
| 72 | + def get_window_handle(self): |
| 73 | + if self.winfo_id() > 0: |
| 74 | + return self.winfo_id() |
| 75 | + else: |
| 76 | + raise Exception("Couldn't obtain window handle") |
| 77 | + def message_loop_work(self): |
| 78 | + try: |
| 79 | + cef.MessageLoopWork() |
| 80 | + self.after(10, self.message_loop_work) |
| 81 | + except Exception as ex: |
| 82 | + tkinter.messagebox.showwarning("message_loop_work",str(ex)) |
| 83 | + def on_configure(self,event): |
| 84 | + try: |
| 85 | + print("on_configure") |
| 86 | + if not self.browser: |
| 87 | + print("embed_browser") |
| 88 | + self.embed_browser(event.width,event.height) |
| 89 | + else: |
| 90 | + print("on_mainframe_configure") |
| 91 | + self.on_mainframe_configure(event.width,event.height) |
| 92 | + except Exception as ex: |
| 93 | + tkinter.messagebox.showwarning("on_configure",str(ex)) |
| 94 | + def on_root_configure(self): |
| 95 | + try: |
| 96 | + if self.browser: |
| 97 | + self.browser.NotifyMoveOrResizeStarted() |
| 98 | + except Exception as ex: |
| 99 | + tkinter.messagebox.showwarning("on_root_configure",str(ex)) |
| 100 | + def on_mainframe_configure(self, width, height): |
| 101 | + try: |
| 102 | + if self.browser: |
| 103 | + ctypes.windll.user32.SetWindowPos(self.browser.GetWindowHandle(), 0, 0, 0, width, height, 0x0002) |
| 104 | + self.browser.NotifyMoveOrResizeStarted() |
| 105 | + except Exception as ex: |
| 106 | + tkinter.messagebox.showwarning("on_mainframe_configure",str(ex)) |
| 107 | + def on_focus_in(self, _): |
| 108 | + if self.browser: |
| 109 | + self.browser.SetFocus(True) |
| 110 | + def on_focus_out(self, _): |
| 111 | + if self.browser: |
| 112 | + self.browser.SetFocus(False) |
| 113 | + def on_root_close(self): |
| 114 | + if self.browser: |
| 115 | + self.browser.CloseBrowser(True) |
| 116 | + self.clear_browser_references() |
| 117 | + self.destroy() |
| 118 | + cef.Shutdown() |
| 119 | + ThisDialog.destroy() |
| 120 | + def clear_browser_references(self): |
| 121 | + self.browser = None |
| 122 | +#WEB浏览器控件 |
| 123 | +class Webbrowser: |
| 124 | + def __init__(self): |
| 125 | + self.URL = "" |
| 126 | + self.URLArray = [] |
| 127 | + self.URLIndex = -1 |
| 128 | + self.WebFrame = None |
| 129 | + self.URLEntryVariable = None |
| 130 | + self.thread = None |
| 131 | + self.Width = 1000 |
| 132 | + self.Height = 600 |
| 133 | + print("Webbrowser") |
| 134 | + #设置Entry |
| 135 | + def setURLEntryVariable(self,entrytextvariable): |
| 136 | + print("set_Entry") |
| 137 | + self.URLEntryVariable = entrytextvariable |
| 138 | + #设置Frame |
| 139 | + def set_Frame(self,frame): |
| 140 | + print("set_Frame") |
| 141 | + self.WebFrame = BrowserFrame(frame) |
| 142 | + #设置URL |
| 143 | + def set_URL(self,url): |
| 144 | + if self.URL != url: |
| 145 | + self.URL = url |
| 146 | + self.URLArray.append(url) |
| 147 | + self.URLIndex = self.URLIndex + 1 |
| 148 | + print("set_URL:%d"%(self.URLIndex)) |
| 149 | + print(self.URLArray) |
| 150 | + if self.WebFrame != None: |
| 151 | + self.WebFrame.setURL(self.URL) |
| 152 | + #获取源代码 |
| 153 | + def getSourceCode(self): |
| 154 | + if self.WebFrame != None: |
| 155 | + return self.WebFrame.getSourceCode() |
| 156 | + return None |
| 157 | + #后退 |
| 158 | + def goBack(self): |
| 159 | + print("goBack:%d"%(self.URLIndex)) |
| 160 | + if self.WebFrame != None: |
| 161 | + if self.URLIndex > 0: |
| 162 | + self.URLIndex = self.URLIndex - 1 |
| 163 | + url = self.URLArray[self.URLIndex] |
| 164 | + print(url) |
| 165 | + self.WebFrame.setURL(url) |
| 166 | + self.URLEntryVariable.set(url) |
| 167 | + #是否可以后退 |
| 168 | + def cangoBack(self): |
| 169 | + if self.URLIndex > 0: |
| 170 | + return True |
| 171 | + return False |
| 172 | + #前进 |
| 173 | + def goForward(self): |
| 174 | + print("GoForward:%d"%(self.URLIndex)) |
| 175 | + if self.WebFrame != None: |
| 176 | + if self.URLIndex < len(self.URLArray)-1: |
| 177 | + self.URLIndex = self.URLIndex + 1 |
| 178 | + url = self.URLArray[self.URLIndex] |
| 179 | + print(url) |
| 180 | + self.WebFrame.setURL(url) |
| 181 | + self.URLEntryVariable.set(url) |
| 182 | + #是否可以后退 |
| 183 | + def cangoForward(self): |
| 184 | + if self.URLIndex < len(self.URLArray)-1: |
| 185 | + return True |
| 186 | + return False |
| 187 | + #刷新 |
| 188 | + def refresh(self): |
| 189 | + print("refresh:%d"%(self.URLIndex)) |
| 190 | + if self.WebFrame != None: |
| 191 | + self.WebFrame.setURL(self.URLArray[self.URLIndex]) |
| 192 | + #主页 |
| 193 | + def gohome(self): |
| 194 | + print("gohome:%d"%(self.URLIndex)) |
| 195 | + self.set_URL("www.py-me.com") |
| 196 | + self.URLEntryVariable.set("www.py-me.com") |
| 197 | + #设置Frame |
| 198 | + def resetSize(self,W,H): |
| 199 | + self.Width = W |
| 200 | + self.Height = H |
| 201 | + if self.WebFrame != None: |
| 202 | + self.WebFrame.place(x=0,y=0,width=W,height=H) |
| 203 | + |
| 204 | + |
| 205 | +ThisDialog = tkinter.Tk() |
| 206 | +#ThisDialog.wm_overrideredirect(1) |
| 207 | +ThisDialog.attributes("-toolwindow", 1) |
| 208 | +ThisDialog.title("微信登录PyMe") |
| 209 | +ThisDialog.resizable(0,0) |
| 210 | +ThisDialog.attributes("-topmost", 1) |
| 211 | +ThisDialog.config(bg='#000000') |
| 212 | +Form = tkinter.Canvas(ThisDialog,bg = '#FFFFFF',width = 340,height=360,highlightthickness=0,bd=0) |
| 213 | +Form.pack(expand=1, fill='both') |
| 214 | +QRCodeWebbrowser = Webbrowser() |
| 215 | +QRCodeLabel = tkinter.Frame(Form,bg = '#FFFFFF',width = 100,height = 200) |
| 216 | +QRCodeLabel.place(x = 20,y = 50,width = 300,height = 300) |
| 217 | +QRCodeWebbrowser.set_Frame(QRCodeLabel) |
| 218 | +QRCodeWebbrowser.set_URL(TargetURL) |
| 219 | +QRCodeWebbrowser.resetSize(300,300) |
| 220 | + |
| 221 | + |
| 222 | +#检测源代码 |
| 223 | +def CheckSourceCode(code): |
| 224 | + global ThisDialog |
| 225 | + if code.find('login_success.png') > 0: |
| 226 | + title_begin = code.find('<title>') |
| 227 | + title_end = code.find('</title>') |
| 228 | + title_text = code[title_begin+7:title_end] |
| 229 | + title_splitarray = title_text.split(',') |
| 230 | + userid = title_splitarray[0] |
| 231 | + wechat_nickname = title_splitarray[1].replace('\'','"') |
| 232 | + wechat_token = title_splitarray[2].replace('\'','"') |
| 233 | + client = socket.socket() |
| 234 | + client.connect(('localhost',9656)) |
| 235 | + data = userid + "," +wechat_nickname+","+wechat_token |
| 236 | + client.send(data.encode('utf-8')) |
| 237 | + client.close() |
| 238 | + ThisDialog.destroy() |
| 239 | + |
| 240 | +#检查登录成功 |
| 241 | +def CheckLoginSuccess(): |
| 242 | + global ThisDialog |
| 243 | + sourceCode = QRCodeWebbrowser.getSourceCode() |
| 244 | + sourceCode.SetCallBack(CheckSourceCode) |
| 245 | + ThisDialog.after(3,CheckLoginSuccess) |
| 246 | + |
| 247 | +ThisDialog.after(3,CheckLoginSuccess) |
| 248 | +#居中显示 |
| 249 | +sx = 0 |
| 250 | +sy = 0 |
| 251 | +sw = ThisDialog.winfo_screenwidth() |
| 252 | +sh = ThisDialog.winfo_screenheight() |
| 253 | +nx = sx + (sw - 340)/2 |
| 254 | +ny = sy + (sh - 360)/2 |
| 255 | +geoinfo = str('%dx%d+%d+%d'%(340,360,nx,ny)) |
| 256 | +ThisDialog.geometry(geoinfo) |
| 257 | +ThisDialog.mainloop() |
0 commit comments