1.安装wxpython
pip install -U wxpython
2.创建一个wx.App的子类
2.1实现步骤
2.2简单实现一个GUI界面
import wx
# 定义一个子类
class App(wx.App):
def OnInit(self):
frame = wx.Frame(parent=None, title="Hello wxpython")
frame.Show()
return True
if __name__ == '__main__':
# 实例化该子类
app = App()
# 调用主循环方法
app.MainLoop()
出现如下窗口
3.直接使用wx.App类
当仅有一个窗口时,直接使用wx.App类会比较简洁
import wx
app = wx.App()
frame = wx.Frame(None, title="Hello wxPython")
frame.Show()
app.MainLoop()
也会出现同样窗口
4.使用wx.Frame框架
import wx
class Myframe(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, title="创建Frame", pos=(100, 100), size=(300, 300), )
if __name__ == '__main__':
app = wx.App()
frame = Myframe(parent=None, id=-1)
frame.Show()
app.MainLoop()
5.StaticText文本类
语法格式
实例
import wx
class Myframe(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, title="创建staticText", pos=(100, 100), size=(600, 400))
panel = wx.Panel(self) # 创建画板
wx.StaticText(panel, label = "python之禅 --lbqdhg", pos=(100,20))
if __name__ == '__main__':
app = wx.App()
frame = Myframe(parent=None, id=-1)
frame.Show()
app.MainLoop()
可以对字体进行设置
实例
import wx
class Myframe(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, title="创建staticText", pos=(100, 100), size=(600, 400))
panel = wx.Panel(self) # 创建画板
title = wx.StaticText(panel, label="python之禅 --lbqdhg", pos=(100, 20))
font = wx.Font(16, wx.DEFAULT, wx.FONTSTYLE_NORMAL, wx.NORMAL)
title.SetFont(font)
wx.StaticText(panel, label="优美胜于丑陋", pos=(50, 50))
if __name__ == '__main__':
app = wx.App()
frame = Myframe(parent=None, id=-1)
frame.Show()
app.MainLoop()
6.TextCtrl输入文本
语法格式
实例
import wx
class Myframe(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, title="创建TextCtrl类", pos=(100, 100), size=(600, 400))
panel = wx.Panel(self)
self.title = wx.StaticText(panel, label = "请输入用户名和密码",pos = (140,20))
self.label_user = wx.StaticText(panel, label="用户名", pos=(50, 50))
self.text_user = wx.TextCtrl(panel, pos=(100,50),size = (235,25),style=wx.TE_LEFT)
self.label_password = wx.StaticText(panel, label="密码", pos=(50, 90))
self.text_password = wx.TextCtrl(panel, pos=(100, 90), size=(235, 25), style=wx.TE_PASSWORD)
if __name__ == '__main__':
app = wx.App()
frame = Myframe(parent=None, id=-1)
frame.Show()
app.MainLoop()
7.Button按钮类
语法格式
实例
import wx
class Myframe(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, title="创建TextCtrl类", pos=(100, 100), size=(600, 400))
panel = wx.Panel(self)
self.title = wx.StaticText(panel, label = "请输入用户名和密码",pos = (140,20))
self.label_user = wx.StaticText(panel, label="用户名", pos=(50, 50))
self.text_user = wx.TextCtrl(panel, pos=(100,50),size = (235,25),style=wx.TE_LEFT)
self.label_password = wx.StaticText(panel, label="密码", pos=(50, 90))
self.text_password = wx.TextCtrl(panel, pos=(100, 90), size=(235, 25), style=wx.TE_PASSWORD)
self.bt_confirm = wx.Button(panel, label="确定",pos=(105,130))
self.bt_cancel = wx.Button(panel,label="取消",pos= (195,130))
if __name__ == '__main__':
app = wx.App()
frame = Myframe(parent=None, id=-1)
frame.Show()
app.MainLoop()
8.Boxsizer
使用Boxsizer
语法格式
实例
import wx
class Myframe(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, title="用户登录", pos=(100, 100), size=(400, 300))
panel = wx.Panel(self)
self.title = wx.StaticText(panel, label="请输入用户名和密码")
vsizer = wx.BoxSizer(wx.VERTICAL)
vsizer.Add(self.title, proportion=0, flag=wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER, border=15)
panel.SetSizer(vsizer)
if __name__ == '__main__':
app = wx.App()
frame = Myframe(parent=None, id=-1)
frame.Show()
app.MainLoop()
实例:使用BoxSizer设置登录界面布局
import wx
class Myframe(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, title="用户登录", pos=(100, 100), size=(400, 300))
panel = wx.Panel(self)
self.title = wx.StaticText(panel, label="请输入用户名和密码")
# 创建button
self.bt_confirm = wx.Button(panel, label="确定")
self.bt_cancel = wx.Button(panel, label="取消")
# 创建文本
self.label_user = wx.StaticText(panel, label="用户名:")
self.text_user = wx.TextCtrl(panel, style=wx.TE_LEFT)
self.label_password = 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_password, 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.ALL, border=5)
hsizer_button.Add(self.bt_cancel, proportion=0, flag=wx.ALL, 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)
if __name__ == '__main__':
app = wx.App()
frame = Myframe(parent=None, id=-1)
frame.Show()
app.MainLoop()
9.绑定事件
语法格式
实例:使用事件判断用户登录
import wx
class Myframe(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, title="用户登录", pos=(100, 100), size=(400, 300))
panel = wx.Panel(self)
self.title = wx.StaticText(panel, label="请输入用户名和密码")
# 创建button
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.label_user = wx.StaticText(panel, label="用户名:")
self.text_user = wx.TextCtrl(panel, style=wx.TE_LEFT)
self.label_password = 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_password, 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.ALL, border=5)
hsizer_button.Add(self.bt_cancel, proportion=0, flag=wx.ALL, 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 == "lbqdhg" and password == "98224":
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()