1

I'm pretty new to Python, so I'll hope you forgive me for such amateurish code. I've tried pouring over examples that do similar things but I'm having trouble figuring out what they're doing that is different. In examples I've seen each button generated with the loop had a different action, for mine only the last button in the loop is affected by the click, no matter which button I press. Here's the code:

import wx
import mmap

class pt:

    Note = open('note.txt', "r+")
    buf = mmap.mmap(Note.fileno(), 0)
    TL = 0
    readline = buf.readline
    while readline():
        TL += 1

class MainWindow(wx.Frame):

    def __init__(self, parent, title):
        w, h = wx.GetDisplaySize()
        x = w * 0
        y = h - bdepth

        wx.Frame.__init__(self, parent, title = title, pos = (x, y), size = (200,bdepth), style = wx.STAY_ON_TOP)

        self.__DoLayout()

        self.Bind(wx.EVT_BUTTON, self.OnClick)

        self.Show(True)

    def __DoLayout(self):

        self.__DoButtons(wx.Panel(self, size=(200,bdepth), pos=(0,0), name='panel'), 'Cheese')

    def __DoButtons(self, panel, label):

        for i, line in enumerate(pt.Note):
            solid = wx.EmptyBitmap(200,50,-1)
            dc = wx.MemoryDC()
            dc.SelectObject(solid)
            solidbrush = wx.Brush(wx.Colour(75,75,75),wx.SOLID)
            solidpen = wx.Pen(wx.Colour(75,75,75),wx.SOLID)
            dc.SetBrush(solidbrush)
            dc.SetPen(solidpen)
            dc.DrawRectangle(0, 0, 200, 50)
            dc.SetTextForeground(wx.Colour(255, 255, 255))
            dc.DrawText(line.rstrip(), 30,  17)
            dc.SelectObject(wx.NullBitmap)

            self.checked = wx.Image('buttonchecked.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap()
            dc = wx.MemoryDC()
            dc.SelectObject(self.checked)
            dc.SetTextForeground(wx.Colour(200, 255, 0))
            dc.DrawText(line.rstrip(), 30,  17)
            dc.SelectObject(wx.NullBitmap)

            self.b = wx.BitmapButton(panel, i + 800, solid, (0,  i * 50), (solid.GetWidth(), solid.GetHeight()), style = wx.NO_BORDER, name=line.rstrip())

    def OnClick(self, event):
        self.b.SetBitmapDisabled(self.checked)
        self.b.Enable(False)
        print('cheese')

bdepth = pt.TL * 50
app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()enter code here

1 Answer 1

1

Only the last button is working because each time you go through the __DoButtons loop you reassign self.b to a different button. So after the loop has finished self.b is only assigned to the last button. You can get the button pressed using the event.GetEventObject() method.

Change your OnClick method to:

def OnClick(self, event):
    button = event.GetEventObject()
    button.SetBitmapDisabled(self.checked)
    button.Enable(False)
    print('cheese')
Sign up to request clarification or add additional context in comments.

3 Comments

That makes a lot of sense. Thank you, that fixed it as well as helping me understand how this works a lot better.
@Tryst:It would also be better to bind each button in the loop in your __DoButtons() method e.g self.Bind(wx.EVT_BUTTON, self.OnClick, self.b)
@volting: Thank you for the suggestion. I originally had it that way, but changed it when I was trying to duplicate an example. It runs slightly more smoothly with your suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.