Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Button press delay solved by inheriting GenButton!! #20

Open
Caue-Aron opened this issue Feb 24, 2022 · 0 comments
Open

Button press delay solved by inheriting GenButton!! #20

Caue-Aron opened this issue Feb 24, 2022 · 0 comments

Comments

@Caue-Aron
Copy link

Caue-Aron commented Feb 24, 2022

I managed to solve the delay of the custom button by inherit from GenButton class instead of Control. Here's a bit of the code:
`import wx
from wx.lib.buttons import GenButton
from wx.lib.newevent import NewCommandEvent

button_event, EVT_BUTTON = NewCommandEvent()

class Button(GenButton):

def __init__(self, parent, id, label='', size=wx.DefaultSize, font=None, color='#a0a0a0'):
    super().__init__(parent, id, label='', size=size, style=wx.NO_BORDER)

    self.mouse_down = False
    self.buffer = None
    self.label = label

    self.SetBackgroundColour(wx.Colour(color))

    if type(font) != wx.Font:
        self.font = self.GetParent().GetFont()
    else:
        self.font = font

def OnPaint(self, evt):
    wx.BufferedPaintDC(self, self.buffer)

def OnSize(self, event):
    size = self.GetClientSize()

    # Make sure size is at least 1px to avoid
    # strange "invalid bitmap size" errors.
    if size[0] < 1:
        size = (1, 1)
    self.buffer = wx.Bitmap(*size)
    self.update()

def update(self):
    dc = wx.MemoryDC()
    dc.SelectObject(self.buffer)
    dc = wx.GCDC(dc)

    self.draw_background(dc)
    self.draw_widget(dc)

    del dc

    self.Refresh()
    self.Update()

def draw_background(self, dc):
    thickness = 1
    w, h = self.GetSize()
    dc.SetPen(wx.TRANSPARENT_PEN)
    dc.SetBrush(wx.Brush('white'))
    dc.DrawRectangle(0, 0, w, h)

def draw_widget(self, dc):
    thickness = 1
    w, h = self.GetSize()

    if self.mouse_down:
        dc.SetPen(wx.Pen('#a0a0a0', thickness))
        dc.DrawLine(0, 0, w-1, 0)
        dc.SetPen(wx.Pen(wx.Colour(self.GetBackgroundColour()).ChangeLightness(45), thickness))
        dc.DrawLine(0, thickness, w-2, thickness)

        dc.SetPen(wx.Pen('#a0a0a0', thickness))
        dc.DrawLine(0, 0, 0, h-1)
        dc.SetPen(wx.Pen(wx.Colour(self.GetBackgroundColour()).ChangeLightness(45), thickness))
        dc.DrawLine(thickness, thickness, thickness, h-2)

        dc.SetPen(wx.Pen('white', thickness))
        dc.DrawLine(w, thickness, w, h)
        dc.SetPen(wx.Pen("white", thickness))
        dc.DrawLine(2, h, w - thickness, h)

    else:
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.SetBrush(wx.Brush('white'))
        dc.DrawRectangle(0, 0, w, h)

    dc.SetFont(self.font)
    txt_w, txt_h = dc.GetTextExtent(self.label)
    txt_x = (w - txt_w) / 2
    txt_y = (h - txt_h) / 2

    if self.mouse_down:
        txt_x += thickness
        txt_y += thickness

    # Draw text
    dc.DrawText(self.label, int(txt_x), int(txt_y))

def button_function(self):
    print('no function for button')

def set_function(self, function):
    self.button_function = function

def OnLeftDown(self, evt):
    self.mouse_down = True
    self.update()
    self.button_function()
    self.send_event(True)

def OnLeftUp(self, evt):
    self.mouse_down = False
    self.update()
    self.send_event(False)

def send_event(self, pressed):
    wx.PostEvent(self, button_event(id=self.GetId(), value=pressed))`
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant