Search code examples
wxpython

how can I write a text to speech code which will read the message on each panel in wxpython


Here is my code I want to bind to two buttons.How can i get both method to run when i click on the saving or checking button?

    saving = wx.Button(self.panel, label = "Savings", pos = (150,240))
    sizer.Add(saving,0,flag=wx.ALL, border=5)
    credit = wx.Button(self.panel, label = "Credit", pos = (200,280))
    sizer.Add(credit,0,flag=wx.ALL, border=5)

def withdraw_amount(self, amount, acc_type):
    if acc_type=='saving' and amount<=self.saving_amount:
        self.saving_amount -= amount
    elif acc_type=='chequing' and amount<=self.chequing_amount:
        self.chequing_amount -= amount
    else:
        print("Not Enough Balance or invalid account type!")
        return False
    return True


 def deposite_amount(self, amount, acc_type):
    if acc_type=='saving':
        self.saving_amount += amount
    elif acc_type=='chequing':
        self.chequing_amount += amount
    else:
        print("Invalid account type")
        return False
    return True

Solution

  • Hopefully this will point you in the right direction, for common methods of binding a button to a callback function.

    #!/usr/bin/env python
    import wx
    
    class MyFrame(wx.Frame):
        def __init__(self, *args, **kwargs):
            wx.Frame.__init__(self, *args, **kwargs)
    
            self.btn = wx.Button(self, id=-1, label="One")
            self.btn.Bind(wx.EVT_BUTTON, lambda event, b_name=self.btn.GetLabel(), b_id=self.btn.GetId(): self.OnButton(event, b_name, b_id) )
    
            self.btn2 = wx.Button(self, id=-1, label="Two")
            self.btn2.Bind(wx.EVT_BUTTON, self.OnButton2)
    
            self.btn3 = wx.Button(self, id=-1, label="Three")
            self.btn3.Bind(wx.EVT_BUTTON, self.OnButton3)
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(self.btn, 0, wx.ALL, 10)
            sizer.Add(self.btn2, 0, wx.ALL, 10)
            sizer.Add(self.btn3, 0, wx.ALL, 10)
            self.SetSizer(sizer)
    
            self.account_type= "Deposit"
    
        def OnButton(self, event, button_label, button_id):
            print ("\nInformation provided via a Lambda")
            print ("Button Pressed:", button_label, button_id)
    
        def OnButton2(self, event):
            button_label = self.btn2.GetLabel()
            button_id = self.btn2.GetId()
            account_type = self.account_type
            print ("\nInformation provided by self.item Query")
            print ("Button2 Pressed:", button_label, button_id)
            print ("Value of account type:", account_type)
    
        def OnButton3(self, event):
            obj = event.GetEventObject()
            button_label = obj.GetLabel()
            button_id = obj.GetId()
            print ("\nInformation provided via eventobject")
            print ("Button3 Pressed:", button_label, button_id)
    
    app = wx.App(False)
    frame = MyFrame(None, title="Button Bind")
    frame.Show()
    app.MainLoop()