I am trying to make a 8 x 8 grid of boxes to manipulate for a matching game. I started with wxPython since I have no experience with pygame. I have it laid out with:

WINDOW_WIDTH = 1200
WINDOW_HEIGHT = 1200

class MainFrame(wx.Frame):

	def __init__(self):
		wx.Frame.__init__(self, None, title = 'matching game',
				pos = (200, 75), size = (WINDOW_WIDTH, WINDOW_HEIGHT)) 
                
                self.background = wx.Panel(self)

		self.box = wx.Button(self.background, (..picture?))

So, I need 64 150 x 150 clickable boxes that i can bind stuff to. I'm not sure which direction to go with this. Loops? and if so, how do i define the position of each box inside a loop?

Also wondering if i should attach permanent pictures to each box1 - box32 (since there will be 2 of each on the board at a time) and shuffle the numbers, or shuffle the pictures among the boxs.

Possibly make a switch to pygame?

Or this may just be a bit over my head. Thanks regardless

Recommended Answers

All 3 Replies

Well what i would do is make a hell of a lot of buttons.. well only 32, then i would use a loop to make a list of all of them something like:

for f in range(64):
    buttons.append(wx.Button(self, wx.ID_ANY, label = str(f)))

That takes care of making the buttons. Then i would make sizers!

horsizers = []
mainsizer = wx.BoxSizer(wx.VERTICAL)
index = 0
for f in range(8):
    size = wx.BoxSizer()
    for g in range(8):
        size.Add(buttons[index], proportion = 0)
        index += 1
    horsizers.append(size)

for size in horsizers:
    mainsizer.Add(size, proportion = 0)

self.SetSizer(mainsizer)

That just gives a general outline of how to do this... im not actually sure if it works.

First of all, do yourself and the rest of us a big favor and do not use tabs for indentations. As you can see, your code shows up badly in the Daniweb code area. Most of us use 4 spaces for indentation.

Here is an example of wxPython's GridSizer to display BitmapButtons in a nice grid. The bitmap images in this case are provided by wxPython's ArtProvider:

# exploring wxPython's GridSizer, ArtProvider and BitmapButton

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, art_list):
        wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle,
            size=(360, 255))
        self.SetBackgroundColour('green')
        
        self.art_list = art_list
        
        # main sizer forms a border
        vsizer = wx.BoxSizer(wx.VERTICAL)
        
        # wx.GridSizer(rows, cols, vgap, hgap)
        # art_list has 48 items so make it 6x8
        gsizer = wx.GridSizer(6, 8, 5, 5)

        self.btn = range(len(self.art_list))
        # fill the drid sizer using a loop
        for ix, art in enumerate(self.art_list):
            # set up a consecutive unique id for each button
            id = 1000 + ix
            bmp = wx.ArtProvider.GetBitmap(art, wx.ART_OTHER, (16, 16))
            self.btn[ix] = wx.BitmapButton(self, id, bitmap=bmp)
            # the gridsizer fills left to right one row at a time
            gsizer.Add(self.btn[ix], 0, wx.ALL|wx.EXPAND, border=2)
            self.btn[ix].Bind(wx.EVT_BUTTON, self.btnClick)

        # add to the main sizer and set
        vsizer.Add(gsizer, 0, wx.EXPAND|wx.ALL, border=10)
        self.SetSizer(vsizer)

    def btnClick(self, event):
        """do something"""
        # get the art_id of the button clicked from the art_list
        art_id = self.art_list[event.GetId() - 1000]
        # show the art_id in the frame title bar
        self.SetTitle(str(art_id))


# constants of art items in ArtProvider 
art_list = [
wx.ART_ADD_BOOKMARK, 
wx.ART_DEL_BOOKMARK, 
wx.ART_HELP_SIDE_PANEL,
wx.ART_HELP_SETTINGS, 
wx.ART_HELP_BOOK, 
wx.ART_HELP_FOLDER,
wx.ART_HELP_PAGE, 
wx.ART_GO_BACK, 
wx.ART_GO_FORWARD,
wx.ART_GO_UP,
wx.ART_GO_DOWN,
wx.ART_GO_TO_PARENT,
wx.ART_GO_HOME,
wx.ART_FILE_OPEN,
wx.ART_FILE_SAVE,
wx.ART_FILE_SAVE_AS,
wx.ART_PRINT,
wx.ART_HELP,
wx.ART_TIP,
wx.ART_REPORT_VIEW,
wx.ART_LIST_VIEW,
wx.ART_NEW_DIR,
wx.ART_HARDDISK,
wx.ART_FLOPPY,
wx.ART_CDROM,
wx.ART_REMOVABLE,
wx.ART_FOLDER,
wx.ART_FOLDER_OPEN,
wx.ART_GO_DIR_UP,
wx.ART_EXECUTABLE_FILE,
wx.ART_NORMAL_FILE,
wx.ART_TICK_MARK,
wx.ART_CROSS_MARK,
wx.ART_ERROR,
wx.ART_QUESTION,
wx.ART_WARNING,
wx.ART_INFORMATION,
wx.ART_MISSING_IMAGE,
wx.ART_COPY,
wx.ART_CUT,
wx.ART_PASTE,
wx.ART_DELETE,
wx.ART_NEW,
wx.ART_UNDO,
wx.ART_REDO,
wx.ART_QUIT,
wx.ART_FIND,
wx.ART_FIND_AND_REPLACE
]

app = wx.App()
# create MyFrame instance and show the frame
MyFrame(None, "wx.ArtProvider test", art_list).Show()
app.MainLoop()

Apologies for the tabbing. This is perfect though.

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.