I want to be able to resize an html window but keep the scroll position. This example almost works, but flickers.
To test:
- load a good sized html file using the Load Html File button
- scroll down
- resize the window.
The window keeps its same position through resizes, but it flickers horribly. The code for htmlwindow is resetting the scroll position to 0 each resize, I think. I'd like to keep it from redrawing until the scroll position is fixed in the post_resize function.
I've tried various combinations of freeze/thaw and trying to hook into the paint events, but have had no success. Suggestions?
import wx
import wx.html
_POST_RESIZE_EVENT = wx.NewEventType()
class _PostResizeEvent(wx.PyEvent):
def __init__(self, pos):
wx.PyEvent.__init__(self)
self.SetEventType(_POST_RESIZE_EVENT)
self.pos = pos
def EVT_POST_RESIZE(win, func):
win.Connect(-1, -1, _POST_RESIZE_EVENT, func)
class MyHtmlPanel(wx.Panel):
"""
class MyHtmlPanel inherits wx.Panel and adds a button and HtmlWindow
"""
def __init__(self, parent, id):
# default pos is (0, 0) and size is (-1, -1) which fills the frame
wx.Panel.__init__(self, parent, id)
self.SetDoubleBuffered(True)
self.SetBackgroundColour("yellow")
self.html1 = wx.html.HtmlWindow(self, id, pos=(0,30), size=(602,310))
self.btn1 = wx.Button(self, -1, "Load Html File", pos=(0,0))
self.btn1.Bind(wx.EVT_BUTTON, self.OnLoadFile)
self.btn2 = wx.Button(self, -1, "Clear Page", pos=(120,0))
self.btn2.Bind(wx.EVT_BUTTON, self.OnClearPage)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.btn1, 0)
sizer.Add(self.btn2, 0)
sizer.Add(self.html1, 1, wx.EXPAND)
self.SetSizer(sizer)
self.html1.Bind(wx.EVT_SCROLLWIN, self.OnScroll)
self.Bind(wx.EVT_SIZE, self.OnSize)
EVT_POST_RESIZE(self.html1, self.post_resize)
self.hist=0
def OnScroll(self, evt):
self.hist = self.html1.GetViewStart()[1]
evt.Skip()
def OnSize(self, evt):
wx.PostEvent(self.html1, _PostResizeEvent(self.hist))
evt.Skip()
def post_resize(self, evt):
self.html1.Scroll(0, evt.pos)
def OnLoadFile(self, event):
dlg = wx.FileDialog(self, wildcard = '*.html', style=wx.OPEN)
if dlg.ShowModal():
path = dlg.GetPath()
self.html1.LoadPage(path)
dlg.Destroy()
def OnClearPage(self, event):
self.html1.SetPage("")
app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID, title, size
frame = wx.Frame(None, -1, "HtmlWindow()", size=(610, 380))
# call the derived class, -1 is default ID
MyHtmlPanel(frame,-1)
# show the frame
frame.Show(True)
# start the event loop
app.MainLoop()