luckrill wrote:
Display image with scrollpanel and DC, This problem is too difficult
for me.
I search and read many scroll code, did not find the answers I need.
Now, I need step by step to debug and learn scroll panel.
I'm not sure how a DC gets involved in this at all. Here is the very
primitive image viewer tool I write, using a ScrolledWindow. It's not a
pinnacle of shining coding style, but it works. You will need to change
the definition of "BASE" to tell it what default directory to start in.
You can right-click on a file to do a few simple operations (delete,
rename, move, launch in external app).
import os
import wx
BASE = "c:/tmp"
mappings = {
'.bmp': wx.BITMAP_TYPE_BMP,
'.jpg': wx.BITMAP_TYPE_JPEG,
'.jpeg': wx.BITMAP_TYPE_JPEG,
'.png': wx.BITMAP_TYPE_PNG,
'.gif': wx.BITMAP_TYPE_GIF
}
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__( self, None, -1, 'Image Viewer', size=(1024,768) )
# Create the controls.
split = wx.SplitterWindow( self, -1, style=wx.SP_LIVE_UPDATE )
pnl = wx.ScrolledWindow( split, -1, style=wx.SUNKEN_BORDER )
pnl.SetScrollbars( 50, 50, 20, 20 )
self.img = wx.StaticBitmap( pnl, -1 )
self.imgpanel = pnl
left = wx.Panel( split, -1, style=wx.SUNKEN_BORDER );
dirBox = wx.GenericDirCtrl( left, -1,
style=wx.SUNKEN_BORDER | wx.DIRCTRL_DIR_ONLY )
self.fileBox = wx.ListBox( left, -1 )
split.SetMinimumPaneSize( 20 )
split.SplitVertically ( left, pnl, 200 )
# Do the layout.
sizer = wx.BoxSizer( wx.VERTICAL )
sizer.Add( dirBox, 1, wx.EXPAND, 0 )
sizer.Add( (10, 10), 0, wx.EXPAND )
sizer.Add( self.fileBox, 3, wx.EXPAND, 0 )
left.SetAutoLayout(1)
left.SetSizer( sizer )
self.Layout()
# Create the popup menu.
popup = wx.Menu()
id = wx.NewId()
popup.Append( id, "&Delete" )
self.Bind( wx.EVT_MENU, self.OnMenuDelete, id=id )
id = wx.NewId()
popup.Append( id, "&Rename..." )
self.Bind( wx.EVT_MENU, self.OnMenuRename, id=id )
id = wx.NewId()
popup.Append( id, "&Move to folder..." )
self.Bind( wx.EVT_MENU, self.OnMenuMove, id=id )
id = wx.NewId()
popup.Append( id, "&Launch" )
self.Bind( wx.EVT_MENU, self.OnMenuLaunch, id=id )
self.popup = popup
# Handle events.
self.Bind( wx.EVT_TREE_SEL_CHANGED, self.OnDir,
dirBox.GetTreeCtrl() )
self.Bind( wx.EVT_LISTBOX, self.OnFile, self.fileBox )
self.fileBox.Bind( wx.EVT_RIGHT_UP, self.OnRightClick )
self.fileBox.Bind( wx.EVT_KEY_UP, self.OnKeyUp )
dirBox.Bind( wx.EVT_KEY_UP, self.OnKeyUp )
# Set the initial state.
self.path = BASE
dirBox.SetPath( self.path )
self.FillFileBox()
def FillFileBox( self ):
knownfmts = mappings.keys()
files = [k for k in os.listdir( self.path )
if os.path.splitext(k)[1].lower() in knownfmts]
self.fileBox.Clear()
self.fileBox.InsertItems( files, 0 )
def OnDir( self, evt ):
db = evt.GetEventObject().GetParent()
self.path = db.GetPath()
self.FillFileBox()
evt.Skip()
def getFile( self ):
return os.path.join( self.path, self.fileBox.GetStringSelection() )
def RefillBox( self ):
ordinal = self.fileBox.GetSelection()
self.FillFileBox()
self.fileBox.SetSelection( ordinal )
def OnFile( self, evt ):
sel = self.getFile()
ext = os.path.splitext( sel )[1].lower()
if not mappings.has_key( ext ):
return
bmp = wx.Image( sel, mappings[ext] ).ConvertToBitmap()
sz = (bmp.GetWidth(), bmp.GetHeight())
self.img.SetSize( sz )
self.img.SetBitmap( bmp )
self.imgpanel.SetVirtualSize( sz )
def OnRightClick( self, evt ):
lb = evt.GetEventObject()
lb.PopupMenu( self.popup, evt.GetPosition() )
def OnMenuDelete( self, evt ):
sel = self.getFile()
os.remove( sel )
self.RefillBox()
self.OnFile( None )
def OnMenuRename( self, evt ):
old = self.fileBox.GetStringSelection()
dlg = wx.TextEntryDialog( self, 'Enter new name:', 'Rename', old )
dlg.SetValue( old )
if dlg.ShowModal() == wx.ID_OK:
os.rename(
self.getFile(),
os.path.join( self.path, dlg.GetValue() )
)
self.RefillBox()
self.OnFile( None )
dlg.Destroy()
def OnMenuMove( self, evt ):
dlg = wx.DirDialog(self, "Choose new directory:",
style=wx.DD_DEFAULT_STYLE|wx.DD_NEW_DIR_BUTTON )
dlg.SetPath( self.path )
if dlg.ShowModal() == wx.ID_OK:
old = self.getFile()
base = os.path.split( old )[1]
new = os.path.join( dlg.GetPath(), base )
if old <> new:
os.rename( old, new )
self.RefillBox()
self.OnFile( None )
dlg.Destroy()
def OnMenuLaunch( self, evt ):
os.system( self.getFile() )
def OnKeyUp( self, evt ):
keycode = evt.GetKeyCode()
if keycode == wx.WXK_ESCAPE:
self.Destroy()
app = wx.App(0)
fr = MyFrame( app )
fr.Show()
app.MainLoop()
···
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.