xwPython的GUI界面的按鈕的運用與反饋
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_cancel = wx.Button(panel, label='取消')
self.bt_cancel.Bind(wx.EVT_BUTTON, self.OnclickCancel)
# 創建文本,左對齊
self.title = wx.StaticText(panel, label="請輸入用戶名和密碼")
self.label_user = wx.StaticText(panel, label="用戶名:")
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_cancel, proportion=0, flag=wx.ALIGN_CENTER, border=5)
# 添加容器,容器中控件縱向排列
vsizer_all = wx.BoxSizer(wx.VERTICAL)
vsizer_all.Add(self.title, proportion=0, flag=wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER,
border=15)
vsizer_all.Add(hsizer_user, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=45)
vsizer_all.Add(hsizer_pwd, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=45)
vsizer_all.Add(hsizer_button, proportion=0, flag=wx.ALIGN_CENTER | wx.TOP, border=15)
panel.SetSizer(vsizer_all)
def OnclickSubmit(self,event):
""" 單擊確定按鈕,執行方法 """
message = ""
username = self.text_user.GetValue() # 獲取輸入的用戶名
password = self.text_password.GetValue() # 獲取輸入的密碼
if username == "" or password == "" : # 判斷用戶名或密碼是否為空
message = '用戶名或密碼不能為空'
elif username =='xiaohao' and password =='socomeon': # 用戶名和密碼正確
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) # 實例化MyFrame類,並傳遞參數
frame.Show() # 顯示窗口
app.MainLoop() # 調用主循環方法