事件处理

#_author:来童星
#date:2019/12/21
# 事件处理
import wx
class MyFrame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'用户登录',size=(400,300))
# 创建画板
panel=wx.Panel(self)

# 创建确定和取消按钮,并绑定事件
self.bt_confirm = wx.Button(panel,label='确定')
self.bt_confirm.Bind(wx.EVT_BUTTON,self.OnclickSubmit)
self.bt_canel = wx.Button(panel, label='取消')
self.bt_canel.Bind(wx.EVT_BUTTON, self.OnclickCancel)


self.title=wx.StaticText(panel,label='请输入用户名和密码')
self.label_user=wx.StaticText(panel,lable='用户名:')
self.text_user=wx.TextCtrl(panel,style=wx.TE_LEFT)
self.label_pwd=wx.StaticText(panel,label='密 码:')
self.text_password=wx.TextCtrl(panel,style=wx.TE_PASSWORD)


# 添加容器,容器中控件横向排列
hsizer_user=wx.BoxSizer(wx.HORIZONTAL)
hsizer_user.Add(self.label_user,proportion=0,flag=wx.ALL,border=5)
hsizer_user.Add(self.text_user,proportion=1,flag=wx.ALL,border=5)

hsizer_pwd=wx.BoxSizer(wx.HORIZONTAL)
hsizer_pwd.Add(self.label_pwd,proportion=0,flag=wx.ALL,border=5)
hsizer_pwd.Add(self.text_password,proportion=1,flag=wx.ALL,border=5)

hsizer_button=wx.BoxSizer(wx.HORIZONTAL)
hsizer_button.Add(self.bt_confirm,proportion=0,flag=wx.ALIGN_CENTER,border=5)
hsizer_button.Add(self.bt_canel,proportion=0,flag=wx.ALIGN_CENTER,border=5)

# 添加容器,容器中控件纵向排列
vsize_all=wx.BoxSizer(wx.VERTICAL)
vsize_all.Add(self.title,proportion=0,flag=wx.BOTTOM|wx.TOP|wx.ALIGN_CENTER,border=5)
vsize_all.Add(hsizer_user,proportion=0,flag=wx.EXPAND|wx.TE_LEFT|wx.RIGHT,border=45)
vsize_all.Add(hsizer_pwd,proportion=0,flag=wx.EXPAND|wx.TE_LEFT|wx.RIGHT,border=45)
vsize_all.Add(hsizer_button,proportion=0,flag=wx.ALIGN_CENTER|wx.TOP,border=15)

panel.SetSize(vsize_all)



def OnclickSubmit(self,event):
'''单击 确定 按钮执行方法'''
message=''
username=self.text_user.GetValue()# 获取输入的用户名
password=self.text_password.GetValue()# 获取输入的密码
if username=='' or password=='':# 判断用户名和密码是否为空
message='用户名和密码不能为空'
elif username=='mr' and password=='mrsoft':
message='登陆成功'
else:
message='用户名和密码不匹配'
wx.MessageBox(message)# 弹出提示框

def OnclickCancel(self,event):
'''单击 取消 按钮执行方法'''
self.text_user.SetValue('') # 清空输入的用户名
self.text_password.SetValue('') # 清空输入的密码
if __name__=='__main__':
app=wx.App()
frame=MyFrame(parent=None,id=-1)
frame.Show()
app.MainLoop()
question:
Traceback (most recent call last):
  File "D:/pycharm_file/new_files/learn_python/week9/t1.py", line 68, in <module>
    frame=MyFrame(parent=None,id=-1)
  File "D:/pycharm_file/new_files/learn_python/week9/t1.py", line 13, in __init__
    self.bt_confirm.Bind(wx.EVT_BUTTON,self.OnclickSubmit)
AttributeError: 'MyFrame' object has no attribute 'OnclickSubmit'






原文地址:https://www.cnblogs.com/startl/p/12076500.html