这是我要绑定到两个按钮的代码。当我单击“保存”或“检查”按钮时如何运行两种方法?
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
答案 0 :(得分:0)
希望对于将按钮绑定到回调函数的常用方法,这将为您指明正确的方向。
#!/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()