欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GUI基础应用程序(Python)

程序员文章站 2022-03-26 14:54:48
...

GUI基础应用程序(Python)

一.subWxapp.py

创建子类

#导入wx
import wx
#定义一个子类
class App(wx.App):
    def OnInit(self):
        frame=wx.Frame(parent=None,title="Hello Stitch")
        frame.Show()
        return True

if __name__=='__main__':



    #实例化该子类
    app=App()



    #调用主循环的方法
    app.MainLoop()

    

二.myframe.py

形成框架

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()

三.staticText.py

使用wx.StaticText输出Python之禅

import wx

class MyFrame(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,title="创建static文本",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)
        
if __name__=='__main__':
    app=wx.App()
    frame=MyFrame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()
        

四.textctrl.py

使用textctrl制作用户登录界面

import wx

class MyFrame(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,title="创建textctrl类",pos=(100,100),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_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()

五.button.py

创建确定和取消按钮

import wx
		
class MyFrame(wx.Frame):
	def __init__(self,parent,id):
	    wx.Frame.__init__(self,parent,id,title="创建textctrl类",pos=(100,100),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_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_confiem=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()

六.sizer.py

生成登录界面

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.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_pwd=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=0,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_pwd,proportion=1,flag=wx.ALL,border=5)#proportion改1会占满一行

        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=25)
        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)
        
        #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=25)
       # panel.SetSizer(vsizer)

if __name__=='__main__':
    app=wx.App()
    frame=MyFrame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

七.thing.py

使用事件判断用户登录

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.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_pwd=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_pwd,proportion=1,flag=wx.ALL,border=5)#proportion改1会占满一行

        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=25)
        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)
        
        #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=25)
       # panel.SetSizer(vsizer)
    def OnclickSubmit(self,event):
        message=""
        username=self.text_user.GetValue()
        password=self.text_pwd.GetValue()
        if username==""or password=="":
            message="用户名或密码不能为空"
        elif username=='stitch'and password=="126":
            message="登陆成功"
        else:
            message="用户名和密码不匹配"
        wx.MessageBox(message)
    def OnclickCancel(self,event):
        username=self.text_user.SetValue("")
        password=self.text_pwd.SetValue("")
        
        

if __name__=='__main__':
    app=wx.App()
    frame=MyFrame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()


相关标签: GUI