'2020/11/29'에 해당되는 글 1건

  1. 2020.11.29 wxPython을 이용한 간단한 웹 브라우저 소스 몇 가지

wx,html 모듈을 이용하는 첫째 소스는 http 프로토콜만 지원하고 https 프로토콜은 지원하지 않습니다.

그러나,  wx,html2 모듈을 이용하는 둘째 소스는 http 프로토콜과 https 프로토콜 모두를 지원합니다.

셋째 소스는 둘째 소스에 네비게이터(뒤로 가기, 앞으로 가기) 가능을 추가한 소스입니다.

[1] wx.html 모듈을 이용한 소스: simple_html_browser.py

# -*- coding: utf-8 -*-
#!/usr/bin/env python

# Filename: simple_html_browser.py
# Execute: python simple_html_browser.py
#    or
# Execute: ./simple_html.browser.py

import wx
import wx.html

class MyHtmlDialog(wx.Dialog):
    def __init__(self, parent, title):
        wx.Dialog.__init__(self, parent, -1, title, size=(600,400))
        html = wx.html.HtmlWindow(self)
        if "gtk2" in wx.PlatformInfo:
            html.SetStandardFonts()

        wx.CallAfter(html.LoadPage, "http://www.google.com")

if __name__ == "__main__":
    app = wx.App()
    dlg = MyHtmlDialog(None, "Simple HTML Browser")
    dlg.ShowModal()
    dlg.Destroy()
    app.MainLoop()

 

[2] wx.html2 모듈을 이용한 소스: simple_html2_browser.py

# -*- coding: utf-8 -*-
#!/usr/bin/env python

# Filename: simple_html2_browser.py
# Execute: python simple_html2_browser.py
#    or
# Execute: ./simple_html2_browser.py

import wx
import wx.html2

class MyHtml2Dialog(wx.Dialog):
    def __init__(self, parent, title):
        wx.Dialog.__init__(self, parent, -1, title, size=(600,400))
        html = wx.html2.WebView.New(self)
        if "gtk2" in wx.PlatformInfo:
            html.SetStandardFonts()

        wx.CallAfter(html.LoadURL, "https://www.google.com")

if __name__ == "__main__":
    app = wx.App()
    dlg = MyHtml2Dialog(None, "Simple HTML2 Browser")
    dlg.ShowModal()
    dlg.Destroy()
    app.MainLoop()

 

 

[3] 위의 둘째 것을 쓸 만 한하게 수정한 웹 브라우저 소스: simple_browser_wk.py

# -*- coding: utf-8 -*-
#!/usr/bin/env python

# Filename: simple_browser_wk.py
#
# Execute: python3 simple_browser_wk.py
#    or
# Execute: pythonw simple_browser_wk.py
#    or
# Execute: ./simple_browser_wk.py
#
# Date: 2020.11.29   Version 0.0.4

import wx 
import wx.html2 

class MyBrowser(wx.Frame): 
    def __init__(self, parent, title): 
        super(MyBrowser, self).__init__(parent, title = title,size = (800,400))  
        sizer = wx.BoxSizer(wx.VERTICAL) 

        hsizer = wx.BoxSizer(wx.HORIZONTAL) 
        emptyText = wx.StaticText(self, -1 , "     ", (10, 10), (10, -1))
        emptyText.SetBackgroundColour('#DDDDDD')
        self.addrText = wx.TextCtrl(self, wx.ID_ANY, value="", size=(300, 30))
        self.addrText.SetFont( wx.Font(12, wx.ROMAN, wx.NORMAL, wx.NORMAL)  )
        self.addrText.SetBackgroundColour('white')
        self.addrText.Bind(wx.EVT_CHAR, self.OnCharEvent)
        self.prevButton = wx.Button(self, 1, '<', (10, 10), (20, -1))
        self.prevButton.Bind(wx.EVT_BUTTON, self.OnPrevButton, id=1)
        self.nextButton = wx.Button(self, 1, '>', (10, 10), (20, -1))
        self.nextButton.Bind(wx.EVT_BUTTON, self.OnNextButton, id=1)
        self.homeButton = wx.Button(self, 1, 'Home', (10, 10), (80, -1))
        self.homeButton.Bind(wx.EVT_BUTTON, self.OnHomeButton, id=1)
        self.exitButton = wx.Button(self, 1, 'Exit', (10, 10), (80, -1))
        self.exitButton.Bind(wx.EVT_BUTTON, self.OnCloseButton, id=1)

        hsizer.Add(self.exitButton, 3, wx.EXPAND, 10) 
        hsizer.Add(self.homeButton, 3, wx.EXPAND, 10) 
        hsizer.Add(self.prevButton, 1, wx.EXPAND, 10) 
        hsizer.Add(self.nextButton, 1, wx.EXPAND, 10) 
        hsizer.Add(self.addrText, 30, wx.EXPAND, 10) 
        hsizer.Add(emptyText, 1, wx.EXPAND, 10) 
        sizer.Add(hsizer, 1, wx.EXPAND, 10) 
        self.browser = wx.html2.WebView.New(self) 
        sizer.Add(self.browser, 20, wx.EXPAND, 10) 
        self.SetSizer(sizer) 
        self.SetSize((800, 700)) 
        self.Bind(wx.html2.EVT_WEBVIEW_NAVIGATED, self.OnNavigated)
        self.OpenWebPage("https://www.google.com")

    def OnCharEvent(self, event):
        keycode = event.GetKeyCode()
        controlDown = event.CmdDown()
        altDown = event.AltDown()
        shiftDown = event.ShiftDown()

        if (not shiftDown) and (not altDown) and (not controlDown) and keycode == 13 :      # [Enter] key
            self.OpenWebPage(self.addrText.GetValue())

    def OnCloseButton(self, event):
        self.Destroy()
        quit()

    def OnHomeButton(self, event):
        self.OpenWebPage("https://scripting.tistory.com")
        event.Skip()

    def OnPrevButton(self, event):
         if self.browser.CanGoBack():
             self.browser.GoBack()
         event.Skip()

    def OnNextButton(self, event):
         if self.browser.CanGoForward():
             self.browser.GoForward()
         event.Skip()

    def OnNavigated(self, event): 
        self.addrText.SetLabel(self.browser.CurrentURL)
        # print(self.browser.CurrentTitle)
        self.SetTitle("SimpleBrowser - {0}".format(self.browser.CurrentTitle))
        event.Skip()

    def OpenWebPage(self, addr): 
        self.browser.LoadURL(addr)
        self.SetTitle("Simpl Browser")
        self.addrText.SetLabel(addr)
        self.Show() 


if __name__ == "__main__":	
    ex = wx.App() 
    f = MyBrowser(None,'Simple Btowser') 
    f.Show()
    ex.MainLoop()
    

 

 

 

 

 

Posted by Scripter

댓글을 달아 주세요