GUI界面 此处使用wxpython 需要先pip库
实例1
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之禅——Tim Peters',pos=(100,20)) font = wx.Font(16, wx.DEFAULT, wx.FONTSTYLE_NORMAL, wx.NORMAL) title.SetFont(font) # 创建文本 wx.StaticText(panel, label= '优美胜于丑陋',pos=(50,50)) wx.StaticText(panel, label='明了胜于晦涩',pos=(50,70)) wx.StaticText(panel, label='简洁胜于复杂',pos=(50,90)) wx.StaticText(panel, label='复杂胜于凌乱',pos=(50,110)) wx.StaticText(panel, label='扁平胜于嵌套',pos=(50,130)) wx.StaticText(panel, label='间隔胜于紧凑',pos=(50,150)) wx.StaticText(panel, label='可读性很重要',pos=(50,170)) wx.StaticText(panel, label='即便假借特例的实用性之名,也不可违背这些规则',pos=(50,190)) wx.StaticText(panel, label='不要包容所有错误,除非你确定需要这样做',pos=(50,210)) wx.StaticText(panel, label='当存在多种可能,不要尝试去猜测',pos=(50,230)) wx.StaticText(panel, label='而是尽量找一种,最好是唯一一种明显的解决方案',pos=(50,250)) wx.StaticText(panel, label='虽然这并不容易,因为你不是 Python 之父',pos=(50,270)) wx.StaticText(panel, label='做也许好过不做,但不假思索就动手还不如不做',pos=(50,290)) wx.StaticText(panel, label='如果你无法向人描述你的方案,那肯定不是一个好方案;反之亦然',pos=(50,310)) wx.StaticText(panel, label='命名空间是一种绝妙的理念,我们应当多加利用',pos=(50,330)) if __name__ == '__main__': app = wx.App() # 初始化应用 frame = MyFrame(parent=None,id=-1) # 实例MyFrame类,并传递参数 frame.Show() # 显示窗口 app.MainLoop() # 调用主循环方法
实例2
import wx class MyFrame(wx.Frame): def __init__(self,parent,id): wx.Frame.__init__(self, parent,id, title="创建TextCtrl类",size=(400, 300)) #创建面板 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_pwd = wx.StaticText(panel,pos=(50,90),label="密 码:") 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) # 实例MyFrame类,并传递参数 frame.Show() # 显示窗口 app.MainLoop() # 调用主循环方法
实例3
import wx class MyFrame(wx.Frame): def __init__(self,parent,id): wx.Frame.__init__(self, parent,id, title="创建TextCtrl类",size=(400, 300)) #创建面板 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_pwd = wx.StaticText(panel,pos=(50,90),label="密 码:") 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) # 实例MyFrame类,并传递参数 frame.Show() # 显示窗口 app.MainLoop() # 调用主循环方法
实例4
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_cancel = wx.Button(panel, label='取消') # 创建文本,左对齐 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) if __name__ == '__main__': app = wx.App() # 初始化 frame = MyFrame(parent=None,id=-1) # 实例MyFrame类,并传递参数 frame.Show() # 显示窗口 app.MainLoop() # 调用主循环方法
实例5 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 =='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) # 实例MyFrame类,并传递参数 frame.Show() # 显示窗口 app.MainLoop() # 调用主循环方法
标签:__,12,Python,GUI,StaticText,label,wx,self,panel From: https://www.cnblogs.com/qingdrippy/p/16982986.html