This is not a real program but a feasibility test.

The purpose is to:
1. create a bunch of bitmapbuttons on a scrolling window from the contents of an image directory.

2. When the user clicks on their favorite button something should happen.

The nice part is that 1 is working very well. The sticking point is two. I've tried various tips from the web without much success.

I'm having minor problems in line 7 and 24 but if I can't get line 30 working so I can actually use the newly created buttons those problems won't matter much. (the lines in question are commented in the code with my thoughts or guesses)

#!/usr/bin/python
import wx
import glob


filenames = glob.glob('/home/jim/Documents/python-programming/swick/thumbs/*.jpg')
#filenames =  glob.glob("{*.jpg,*.png}")  not possible ?         
class TestFrame(wx.Frame):
    def __init__(self, parent):
		count = 0
		wx.Frame.__init__(self, parent, title="Loading Images")
        
        # hook some mouse events
		self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
		self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)

		self.p = wx.ScrolledWindow(self, -1, style=wx.TAB_TRAVERSAL)
		self.p.SetScrollRate(10, 10)
		fgs = wx.FlexGridSizer(cols=1, hgap=10, vgap=10)
		self.p.SetSizer(fgs)

		for name in filenames:
			img1 = wx.Image(name, wx.BITMAP_TYPE_ANY)
			img2 = img1.Scale(256, 192)
			#how to us img2 on bitmapbutton ?
			# or import image  and use Image.open and .thumbnail
			# right now I'm using a directory of image that have already been scaled to the size I want so the program works 
			# as far as creating new buttons with images from that directory and adding them to the sizer
			snb = wx.BitmapButton(self.p, count, wx.Bitmap(name, wx.BITMAP_TYPE_ANY))
			#next line is an attempt to actually use the button we just created - this is the main sticking point
# snb appears to be ws._controls.bitmapbutton proxy of <Swig object of type wx.bitmapbutton * # in other words a pointer
			snb.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
# the bind works in so far as I can detect clicks on the button		
	count += 1
			fgs.Add(snb)
		
    def OnLeftDown(self, event):
        """left mouse button is pressed"""
        pt = event.GetPosition()  # position tuple
        print pt
        self.SetTitle('LeftMouse = ' + str(pt))
        eo = GetId(self)
        print eo
        
    def OnRightDown(self, event):
        """right mouse button is pressed"""
        pt = event.GetPosition()
        self.SetTitle('RightMouse = ' + str(pt))

	for child in self.GetChildren() :
		print "ox"
		child.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
		child.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
		child.Bind(wx.EVT_MOTION, self.OnMotion)

        #self.p.SetSizerAndFit(self)
        #self.Fit()

app = wx.PySimpleApp()
frm = TestFrame(None)
frm.Show()
app.MainLoop()

Thanks in advance for your help.

Hello jludeman! This is a really nice and simple test program, is this your first time using wxPython?

Anyways, onto your questions/problems:

I think in line 7 that you can use "./*.jpg" to get all of the .jpg in the current directory, but this is untested so it may not work.

for line 29, you can use
snb = wx.BitmapButton(self.p, count, wx.BitmapFromImage(img2, 1))
instead so you will be able to used your re-sized image in the button.

I'm not sure if you were having this problem but when I was running this program it was throwing an error on line 42. I fixed it by using
eo = event.GetId()
I assume you were trying to get the ID of the button that was pressed?

other than those things, it looks like it works perfectly fine. I'm curious to what you will be doing once the user chooses their fav. button.

Happy Coding!

Yes this my first go at wxpython. I guess I've got about six hours in it now. Plus maybe three hours on the internet or reading the docs.

I first used wxglade which was helpful in understanding panels and sizers and such. I probably won't use wxglade that much. This code was written using Geany.

I changed eo = GetId(self) to eo = event.GetId() and sure enough each bitmapbutton has a unique id.

Using snb = wx.BitmapButton(self.p, count, wx.BitmapFromImage(img2, 1)) works in that it does not create any errors. However the resulting images are extremely distorted.

I tried:
Import Image

img1 = Image.open(name)
# convert to thumbnail image
img1.thumbnail((256, 256), Image.ANTIALIAS)

Now the following code line gives an error
snb = wx.BitmapButton(self.p, count, wx.BitmapFromImage(img1, 1))

type error in method new_BitMapFromImage expected argument 1 of wxImage
const &

img1 is reported as being JpegImagePlugin.JpegImageFile

Thanks for the help.

Oh and I originally intended that if the user picked the right image they would win a new car and a fairly large sailboat. Sadly my credit union informs me that such a thing is not possible due to a funding shortfall.

Actually it was to allow the user to pick a new background image for the Slim login manager.

Thanks to winmic and the internet I've solved all three problems.

Here is the code.

#!/usr/bin/python
import wx
import glob
import os

# define an array to hold image file names
image_file_storage = []

# subroutine to get file names from directory
def image_file_directory(directory_path):
	#change directory to where the large images are
	os.chdir(directory_path)
	# find all jpeg and png files in the directory
	fns00 = glob.glob("*.png")
	fns01 = glob.glob("*.jpg")
	# join the lists into one long one
	# I did not find this one on the web I just tried it and it worked
	fns = fns00 + fns01
	return fns

# subroutine to scale the bitmap I found this on the web
# http://stackoverflow.com/questions/2504143/how-to-resize-and-draw-an-image-using-wxpython
def scale_bitmap(bitmap, width, height):
    image = wx.ImageFromBitmap(bitmap)
    image = image.Scale(width, height, wx.IMAGE_QUALITY_HIGH)
    result = wx.BitmapFromImage(image)
    return result
        
# subroutines to hold and return the image file names - this is a zero based array        
def store_image_file_names(image_file_name):
	image_file_storage.append(image_file_name)		        

def get_image_file_names(index):
	return image_file_storage[index]		        
	
class TestFrame(wx.Frame):
    def __init__(self, parent):
		count = 0
		wx.Frame.__init__(self, parent, title="Swick")
        
        # hook some mouse events
		self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
		self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)

		# add a scrolling window panel to the frame and set the scroll rate
		self.p = wx.ScrolledWindow(self, -1, style=wx.TAB_TRAVERSAL)
		self.p.SetScrollRate(10, 10)
		# add a flex grid sizer to the scrolling window - this will be the container for the new bitmapbuttons
		fgs = wx.FlexGridSizer(cols=1, hgap=10, vgap=10)
		self.p.SetSizer(fgs)
		
		image_folder = image_file_directory("/home/jim/Documents/python-programming/swick/thumbs01/")
		for image_file in image_folder:
			# create current image from file
			img1 = wx.Bitmap(image_file, wx.BITMAP_TYPE_ANY)
			# scale the current image
			img1=scale_bitmap(img1, 256, 192)
			store_image_file_names(image_file)
			# create the new bitmap button with the current image
			snb = wx.BitmapButton(self.p, count, img1)
			# bind the new bitmap button to the left mouse button down event
			snb.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
			# increment the index - used to set the id for the bitmap button
			count += 1
			# add the button to the sizer on the scrolled panel
			fgs.Add(snb)
		
    def OnLeftDown(self, event):
        # left mouse button is pressed
		pt = event.GetPosition()  # position tuple
        # next 2 lines for testing only
        #print pt
        #self.SetTitle('LeftMouse = ' + str(pt))
        # next 3 lines for testing only
		eid = event.GetId()
		test_this_id = get_image_file_names(eid)
		print test_this_id
        
    def OnRightDown(self, event):
        #right mouse button is pressed
        pt = event.GetPosition()  # position tuple

app = wx.PySimpleApp()
frm = TestFrame(None)

frm.Show()
app.MainLoop()

Now I can finish the program. Plus I'm fairly confident that I can use wxpython to develop small but useful applications for linux.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.