Question

So I am a complete beginner at python and usually code in C/C++ or Java. In the process of developing a GUI my events keep getting called at the start of the program running instead of when I click the button. (i know my indents are wrong because I needed to put it into a code block). How do I make my events only get called when I left click the button?

def __init__(self, parent, title):
    super(QuadDash, self).__init__(parent, title=title, size=(1024, 780))            
    self.SetBackgroundColour("white")      
    pnl = wx.Panel(self)
    cbtn = wx.Button(pnl, label='Start Quad', pos=(20,30))
    self.Bind(wx.EVT_LEFT_DOWN, self.start_quad_event(), cbtn)        
    self.Show(True) 

def start_quad_event(self):
    dlg = wx.MessageDialog(self, "Test", "ABC", wx.YES_NO | wx.ICON_QUESTION)
    dlg.ShowModal()


if __name__ == '__main__':
    app = wx.App()
    qd = QuadDash(None, title='QuadCopter Dashboard')
    app.MainLoop()
Était-ce utile?

La solution

The offending line is:

self.Bind(wx.EVT_LEFT_DOWN, self.start_quad_event(), cbtn)

You actually call start_quad_event() and pass the result to bind().

The correct line is:

self.Bind(wx.EVT_LEFT_DOWN, self.start_quad_event, cbtn)

Note: No parentheses. Here you actually pass the function to bind().

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top