1. 期望实现的效果
2. wxformbuilder设计基础界面
在收件人和抄送人文本框输入中文名称或邮箱账号关键词后,期望能够在下面自动弹出一个列表框显示符合条件的邮箱账号信息。
为了实现这个功能在设计界面的时候要在收件人和抄送人两个文本框下面合适的位置配置两个列表框ListBox。
3. 代码实现逻辑
3.1 隐藏检索列表框
打开邮件发送界面时,需要先将两个用于显示检索结果的列表框隐藏起来,直到用户在收件人或抄送人文本框输入检索关键字后再显示出来列表框的检索结果。
代码实现:
class SendMail_Frame(wx.Frame):
flag = True
……
def afterload(self):
# 根据数据情况再次调整控件
self.flag = False
self.m_listBox1.Hide()
self.m_listBox2.Hide()
主操作函数需要能够调用类SendMail_Frame中的afterload方法:
调用SendMail_Frame界面的数字编号为3。
为什么不能够直接使用Hide()函数实现隐藏控件的功能呢?
因为使用Hide()函数实现控件隐藏功能后,再将控件显示出来会造成控件的位置错乱,控件的位置不是开始配置的固定位置,造成界面的不美观。
3.2 设置控件触发事件
设置控件的触发事件:
self.toReceiver_list = []
self.ccReceiver_list = []
# 选择邮件的收件人
# EVT_TEXT:文本因用户的输入或在程序中使用SetValue()而被改变,都要产生该事件。
self.m_textCtrl1.Bind(wx.EVT_TEXT, self.get_mail_list_1)
# 选择邮件的抄送人
self.m_textCtrl2.Bind(wx.EVT_TEXT, self.get_mail_list_2)
# 设置listbox选中item事件
self.m_listBox1.Bind(wx.EVT_LISTBOX, self.set_toReceiver)
self.m_listBox2.Bind(wx.EVT_LISTBOX, self.set_ccReceiver)
事件触发后的执行函数:
def get_mail_list_1(self, event):
mail_info_list = get_mail_dict()
if self.m_textCtrl1.GetValue().strip() == "":
cur_text = self.m_textCtrl1.GetValue()
else:
cur_text = self.m_textCtrl1.GetValue().split(",")[-1]
if self.m_textCtrl1.GetValue().strip() == "":
self.toReceiver_list = []
selected_mail_info = []
for mail_info in mail_info_list:
if cur_text in mail_info:
selected_mail_info.append(mail_info)
self.m_listBox1.SetItems(selected_mail_info)
self.m_listBox1.Show()
def set_toReceiver(self, event):
listbox_value = self.m_listBox1.GetString(self.m_listBox1.GetSelection())
self.toReceiver_list.append(listbox_value)
self.m_textCtrl1.SetValue(",".join(self.toReceiver_list) + ",")
self.m_listBox1.Hide()
抄送人的实现逻辑和收件人的触发事件实现逻辑类似,此处不再赘述。