-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
420 lines (350 loc) · 14.9 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# -*- coding: utf-8 -*-
#没有人比我更懂晓柯浏览器
# XiaoKe v0.2
import wx
import wx.aui
import wx.xrc
import wx.html2
from configparser import ConfigParser
import tkinter as tk
from tkinter import messagebox
import sys
import os
import urllib.parse
import shutil
import re
#获取配置
conf = ConfigParser()
conf.read('config.ini')
Engine = None
if conf['Engine']['Engine'] == 'Default':
Engine = wx.html2.WebViewBackendDefault
elif conf['Engine']['Engine'] == 'IE':
Engine = wx.html2.WebViewBackendIE
elif conf['Engine']['Engine'] == 'Edge':
Engine = wx.html2.WebViewBackendEdge
elif conf['Engine']['Engine'] == 'WebKit':
root = tk.Tk()
root.withdraw()
messagebox.showerror("Error", "webkit浏览器引擎目前不支持!!!")
root.mainloop()
else:
root = tk.Tk()
root.withdraw()
messagebox.showerror("Error", "config.ini的浏览器引擎不支持!!!")
root.mainloop()
###########################################################################
## Class MyFrame1
###########################################################################
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Full Screen Window', style=wx.NO_BORDER | wx.STAY_ON_TOP)
self.ShowFullScreen(False)
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
def OnKeyDown(self, event):
keycode = event.GetKeyCode()
if keycode == wx.WXK_F11:
self.Maximize()
class MyFrame2(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=u"关于晓柯", pos=wx.DefaultPosition,
size=wx.Size(715, 412), style=wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER)
self.icon = wx.Icon('ico.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(self.icon)
self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
self.SetMinSize((715, 412))
self.SetMaxSize((715, 412))
# 禁用最大化和最小化按钮
self.SetWindowStyle(self.GetWindowStyle() & ~(wx.MAXIMIZE_BOX | wx.MINIMIZE_BOX))
bSizer1 = wx.BoxSizer(wx.VERTICAL)
#chgskj.cn
self.browser = wx.html2.WebView.New(self, backend=Engine)
self.browser.LoadURL("https://www.chgskj.cn")
bSizer1.Add(self.browser, 1, wx.EXPAND)
self.SetSizer(bSizer1)
self.Layout()
self.Centre(wx.BOTH)
def __del__(self):
pass
class Tab(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
sizer = wx.BoxSizer(wx.VERTICAL)
self.browser = wx.html2.WebView.New(self, backend=Engine)
self.browser.LoadURL("https://www.baidu.com")
sizer.Add(self.browser, proportion=1, flag=wx.EXPAND)
self.SetSizer(sizer)
class MyFrame1 ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"晓柯浏览器 - 一款轻量高效、性能发挥到位的浏览器", pos = wx.DefaultPosition, size = wx.Size( 1026,747 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.icon = wx.Icon('ico.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(self.icon)
menu_bar = wx.MenuBar()
self.SetMenuBar(menu_bar)
file_menu = wx.Menu()
operation_menu = wx.Menu()
about_menu = wx.Menu()
engine_menu = wx.Menu()
self.star_menu = wx.Menu()
self.history_menu = wx.Menu()
file_menu.Append(wx.ID_FILE6,"打开文件")
file_menu.Append(wx.ID_FILE7,"另存为")
file_menu.Append(wx.ID_FILE8,"打印")
operation_menu.Append(wx.ID_NEW, "新建网页")
operation_menu.Append(wx.ID_CLOSE, "关闭网页")
operation_menu.Append(wx.ID_RESET,"重新加载当前网页")
operation_menu.Append(wx.ID_FORWARD, "下一页")
operation_menu.Append(wx.ID_BACKWARD, "上一页")
about_menu.Append(wx.ID_ABOUT,"关于晓柯浏览器")
engine_menu.Append(wx.ID_FILE1,"IE引擎")
engine_menu.Append(wx.ID_FILE2,"默认引擎")
#engine_menu.Append(wx.ID_FILE3,"WebKit引擎")
engine_menu.Append(wx.ID_FILE4,"Edge引擎")
self.star_menu.Append(wx.ID_FILE5,"收藏当前网页")
self.star_menu.AppendSeparator()
menu_bar.Append(file_menu,"文件")
menu_bar.Append(operation_menu, "编辑")
menu_bar.Append(engine_menu,"引擎")
menu_bar.Append(self.star_menu,"收藏夹")
menu_bar.Append(self.history_menu,"历史记录")
menu_bar.Append(about_menu,"帮助")
self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
bSizer2 = wx.BoxSizer( wx.VERTICAL )
bSizer6 = wx.BoxSizer( wx.HORIZONTAL )
bSizer2.Add( bSizer6, 0, 0, 5 )
bSizer7 = wx.BoxSizer( wx.HORIZONTAL )
self.m_searchCtrl2 = wx.SearchCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_searchCtrl2.ShowSearchButton( True )
self.m_searchCtrl2.ShowCancelButton( False )
bSizer7.Add( self.m_searchCtrl2, 1, wx.EXPAND|wx.TOP|wx.RIGHT|wx.LEFT, 5 )
self.m_button4 = wx.Button( self, wx.ID_ANY, u"搜索", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer7.Add( self.m_button4, 0, wx.TOP|wx.RIGHT|wx.LEFT, 5 )
bSizer2.Add( bSizer7, 0, wx.EXPAND|wx.BOTTOM, 5 )
global bSizer5
bSizer5 = wx.BoxSizer( wx.VERTICAL )
bSizer2.Add( bSizer5, 1, wx.EXPAND, 5 )
#self.browser = wx.html2.WebView.New(self)
#self.browser.LoadURL("https://www.baidu.com")
self.m_searchCtrl2.SetValue("https://www.baidu.com")
#bSizer5.Add( self.browser, 1, wx.ALL|wx.EXPAND, 5 )
# Panel creation and tab holder setup:
global nb
nb = wx.aui.AuiNotebook(self)
# Initiation of the tab windows:
tab = Tab(nb)
# Assigning names to tabs and adding them:
nb.AddPage(tab, "Loading...")
#nb.AddPage()
# Organizing notebook layout using a sizer:
bSizer5.Add(nb, 1, wx.EXPAND)
self.SetSizer( bSizer2 )
self.Layout()
self.Centre( wx.BOTH )
page = nb.GetSelection()
tab = nb.GetPage(page)
self.m_button4.Bind( wx.EVT_BUTTON,self.load_url)
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN,self.load_url)
self.Bind(wx.EVT_MENU, self.new_html, id=wx.ID_NEW)
self.Bind(wx.EVT_MENU, self.close_html, id=wx.ID_CLOSE)
self.Bind(wx.EVT_MENU, self.reset_html, id=wx.ID_RESET)
self.Bind(wx.EVT_MENU, self.forward_html, id=wx.ID_FORWARD)
self.Bind(wx.EVT_MENU, self.backward_html, id=wx.ID_BACKWARD)
self.Bind(wx.EVT_MENU, self.IE_html, id=wx.ID_FILE1)
self.Bind(wx.EVT_MENU, self.Chrome_html, id=wx.ID_FILE2)
#self.Bind(wx.EVT_MENU, self.WebKit_html, id=wx.ID_FILE3)
self.Bind(wx.EVT_MENU, self.Edge_html, id=wx.ID_FILE4)
self.Bind(wx.EVT_MENU, self.about_html, id=wx.ID_ABOUT)
self.Bind(wx.EVT_MENU, self.star_html, id=wx.ID_FILE5)
self.Bind(wx.EVT_MENU, self.open_file, id=wx.ID_FILE6)
self.Bind(wx.EVT_MENU, self.save_file, id=wx.ID_FILE7)
self.Bind(wx.EVT_MENU, self.print_file, id=wx.ID_FILE8)
tab.browser.Bind(wx.html2.EVT_WEBVIEW_NEWWINDOW, self.on_link_clicked)
tab.browser.Bind(wx.html2.EVT_WEBVIEW_LOADED, self.OnLoaded)
tab.browser.Bind(wx.html2.EVT_WEBVIEW_TITLE_CHANGED, self.OnTitleChanged)
self.Bind(wx.EVT_CLOSE, self.OnClose)
self.timer = wx.Timer(self)
self.timer.Start(10)
self.Bind(wx.EVT_TIMER, self.OnTimer)
def __del__( self ):
pass
def parse_url(self,url):
url = str(url)
if os.path.isfile(url):
return url
elif url.startswith('http://') or url.startswith('https://'):
return url
else:
url = 'http://' + url
return url
def OnKeyDown(self, event):
keycode = event.GetKeyCode()
page = nb.GetSelection()
tab = nb.GetPage(page)
url = tab.browser.GetCurrentURL()
if keycode == wx.WXK_F11:
global frame3_url
frame3_url = url
def OnTimer(self,event):
count = nb.GetPageCount()
if count == 0:
self.Close()
wx.GetApp().ExitMainLoop()
def OnClose(self,event):
self.Close()
wx.GetApp().ExitMainLoop()
def star_html(self,event):
page = nb.GetSelection()
tab = nb.GetPage(page)
url = tab.browser.GetCurrentURL()
self.menu_id = wx.Window.NewControlId()
menu_item = self.star_menu.Append(self.menu_id, url)
self.Bind(wx.EVT_MENU, self.open_url, menu_item)
def open_url(self, event):
menu_id = event.GetId()
menu_item = self.star_menu.FindItemById(menu_id)
if menu_item is None:
menu_item = self.history_menu.FindItemById(menu_id)
if menu_item is not None:
menu_item_label = menu_item.GetItemLabel()
page = nb.GetSelection()
tab = nb.GetPage(page)
tab.browser.LoadURL(menu_item_label)
else:
self.warning_message("程序发生了一次错误,错误码:0x01 请你马上到客户群进行反馈")
def is_url(self,string):
regex = re.compile(
r'^https?://' # http:// 或 https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # 域名部分(例如 www.example.com)
r'localhost|' # localhost
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # IP 地址部分
r'(?::\d+)?' # 端口号(可选)
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
return re.match(regex, string) is not None
def about_html(self,event):
frame = MyFrame2(None)
frame.Show()
def update_history(self,event):
page = nb.GetSelection()
tab = nb.GetPage(page)
url = tab.browser.GetCurrentURL()
self.menu_id = wx.Window.NewControlId()
menu_item = self.history_menu.Append(self.menu_id, url)
self.Bind(wx.EVT_MENU, self.open_url, menu_item)
def open_file(self,event):
# 创建一个带有标题和默认文件名的文件对话框
dlg = wx.FileDialog(None, "选择文件", "", "", "*.*", wx.FD_OPEN)
# 如果选择了文件,则打印文件路径
if dlg.ShowModal() == wx.ID_OK:
filepath = dlg.GetPath()
# 关闭对话框
dlg.Destroy()
page = nb.GetSelection()
tab = nb.GetPage(page)
tab.browser.LoadURL(filepath)
def save_file(self,event):
filepath = self.m_searchCtrl2.GetValue()
#判断是不是url
if self.is_url(filepath):
return None
dlg = wx.FileDialog(self, message="Save file as ...", defaultDir=".", defaultFile="", style=wx.FD_SAVE)
#去除掉开头部分
filepath = filepath.replace("file:///", "")
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
shutil.copy(filepath, path)
dlg.Destroy()
def print_file(self,event):
page = nb.GetSelection()
tab = nb.GetPage(page)
tab.browser.Print()
def warning_message(self,str):
root = tk.Tk()
root.withdraw()
answer = messagebox.askyesno("警告", str)
if answer:
# 重启当前程序
python = sys.executable
root.destroy()
os.execl(python, python, *sys.argv)
def IE_html(self,event):
conf.set('Engine', 'Engine', 'IE')
# 保存修改后的ini文件
with open('config.ini', 'w') as config_file:
conf.write(config_file)
self.warning_message("是否切换到IE引擎?")
def Chrome_html(self,event):
conf.set('Engine', 'Engine', 'Default')
# 保存修改后的ini文件
with open('config.ini', 'w') as config_file:
conf.write(config_file)
self.warning_message("是否切换到默认引擎?")
def WebKit_html(self,event):
conf.set('Engine', 'Engine', 'WebKit')
# 保存修改后的ini文件
with open('config.ini', 'w') as config_file:
conf.write(config_file)
self.warning_message("是否切换到WebKit引擎?")
def Edge_html(self,event):
conf.set('Engine', 'Engine', 'Edge')
# 保存修改后的ini文件
with open('config.ini', 'w') as config_file:
conf.write(config_file)
self.warning_message("是否切换到Edge引擎?")
def OnTitleChanged(self,event):
title = event.GetString()
page = nb.GetSelection()
nb.SetPageText(page,title)
def new_html(self,event):
page = Tab(nb)
nb.AddPage(page, "Loading...")
nb.SetSelection(nb.GetPageIndex(page))
page = nb.GetSelection()
tab = nb.GetPage(page)
tab.browser.Bind(wx.html2.EVT_WEBVIEW_NEWWINDOW, self.on_link_clicked)
tab.browser.Bind(wx.html2.EVT_WEBVIEW_LOADED, self.OnLoaded)
tab.browser.Bind(wx.html2.EVT_WEBVIEW_TITLE_CHANGED, self.OnTitleChanged)
def close_html(self,event):
page = nb.GetSelection()
nb.DeletePage(page)
def reset_html(self,event):
page = nb.GetSelection()
tab = nb.GetPage(page)
tab.browser.Reload()
def forward_html(self,event):
page = nb.GetSelection()
tab = nb.GetPage(page)
if tab.browser.CanGoForward():
tab.browser.GoForward()
else:
pass
def backward_html(self,event):
page = nb.GetSelection()
tab = nb.GetPage(page)
if tab.browser.CanGoBack():
tab.browser.GoBack()
else:
pass
def on_link_clicked(self,event):
self.new_html(None)
url = event.GetURL()
page = nb.GetSelection()
tab = nb.GetPage(page)
tab.browser.LoadURL(url)
self.m_searchCtrl2.SetValue(url)
def OnLoaded(self, event):
url = event.GetURL()
url = urllib.parse.unquote(url, 'utf-8') # 解码URL
self.m_searchCtrl2.SetValue(url)
self.update_history(None)
def load_url(self,event):
search_url = self.m_searchCtrl2.GetValue()
search_url = self.parse_url(search_url)
page = nb.GetSelection()
tab = nb.GetPage(page)
tab.browser.LoadURL(search_url)
self.m_searchCtrl2.SetValue(search_url)
app = wx.App(False)
frame = MyFrame1(None)
frame.Show()
app.MainLoop()