Python的GUI程序设计

一、实验目的:

1、熟练掌握Frame窗体的使用;
2、熟练掌握基本控件的用法。
二、实验内容:
1、编写代码实现当改变窗体位置和大小时,除在文本框中显示信息外,还需在状态栏动态变化显示“窗体大小:XXX,XXX;窗体位置:XXX,XXX”。当鼠标在窗体内移动时,文本框会实时显示鼠标的当前坐标,当鼠标移动到窗体之外时,文本框的内容不再变化。程序运行效果如图1所示。
在这里插入图片描述

图1 程序运行效果

2、编写代码创建菜单,当在文本框中输入一个数字,点击菜单项,然后在另一个文本框显示是否为素数。是则显示yes,否则显示No,程序运行效果如图2所示。
在这里插入图片描述

图2 程序运行效果

3、编写代码单选按钮实现性别选择,复选框实现兴趣爱好的选择,并输入文本框中要求的用户名和密码,单击OK按钮会弹出消息框提示输入和选择的内容,单击Cancel按钮自动清除用户的输入,并默认单选按钮性别Male为选中状态。界面效果如图3所示。
在这里插入图片描述

图3 界面效果

程序一:

import wx
class Myframe(wx.Frame):
    def __init__(self,superior):
        wx.Frame.__init__(self,parent=superior,
                          title=u'姓名学号',size=(400,400))
        panel = wx.Panel(self, -1)
        label1 = wx.StaticText(panel, -1, "FrameSize:")
        label2 = wx.StaticText(panel, -1, "FramePos:")
        label3 = wx.StaticText(parent=panel, label="MousePos:")
        self.sizeFrame = wx.TextCtrl(panel, -1, "", style=wx.TE_READONLY)
        self.posFrame = wx.TextCtrl(panel, -1, "", style=wx.TE_READONLY)
        self.posMouse = wx.TextCtrl(panel, -1, "", style=wx.TE_READONLY)
        sizer = wx.FlexGridSizer(3, 2, 5, 5)
        sizer.AddMany(
            [(label1, 0, wx.ALIGN_RIGHT),
             (self.sizeFrame, 0, wx.SHAPED),
             (label2, 0, wx.ALIGN_RIGHT),
             (self.posFrame, 0, wx.ALIGN_RIGHT),
             (label3, 0, wx.EXPAND),
             (self.posMouse, 0, wx.ALIGN_RIGHT)]
        )
        # panel.SetSizer(sizer)

        border = wx.BoxSizer()
        border.Add(sizer, 0, wx.ALL, 15)
        panel.SetSizerAndFit(border)
        self.Fit()    # 窗体大小自适应
        self.Bind(wx.EVT_SIZE, self.OnSize) #响应窗口大小改变

        self.Bind(wx.EVT_MOVE, self.OnFrameMove)  # 响应窗口移动

        panel.Bind(wx.EVT_MOTION, self.OnMouseMove)  # 鼠标移动

    def OnMouseMove(self, event):  # 鼠标移动事件处理函数
        pos = event.GetPosition()
        self.posMouse.SetValue("%s, %s" % (pos.x, pos.y))
    def OnFrameMove(self, event):
        pos = event.GetPosition()
        self.posFrame.SetValue("%s, %s" % (pos.x, pos.y))
    def OnSize(self, event):
        size = event.GetSize()
        self.sizeFrame.SetValue("%s, %s" % (size.width,
                                size.height))
        event.Skip()

if __name__=='__main__':
    app=wx.App()             #创建应用程序对象
    frame=Myframe(None)      #创建框架类对象
    frame.Show(True)         #显示框架
    app.MainLoop()           #事件循环等待与处理

程序二:

import wx,math
class Myframe(wx.Frame):
    def __init__(self,superior):
        wx.Frame.__init__(self,parent=superior,
                          title=u'姓名学号',size=(400,400))
        panel = wx.Panel(self, -1)
        panel.SetBackgroundColour('Yellow')
        label4 = wx.StaticText(parent=panel, label="Input a int:")
        self.primeValue = wx.TextCtrl(panel, -1, "", style=wx.TE_CENTER)
        self.result = wx.StaticText(parent=panel, label="")
        self.buttonCheck = wx.Button(parent=panel, label='Check')
        self.buttonQuit = wx.Button(parent=panel, label='Quit')

        sizer = wx.FlexGridSizer(3, 2, 5, 5)

        sizer.AddMany(
            [(label4, 0, wx.EXPAND),
             (self.primeValue, 0, wx.EXPAND),
             (self.buttonCheck, 0, wx.EXPAND),
             (self.buttonQuit, 0, wx.EXPAND),
             (self.result, 0, wx.ALIGN_RIGHT)])

        # panel.SetSizer(sizer)

        border = wx.BoxSizer()
        border.Add(sizer, 0, wx.ALL, 15)
        panel.SetSizerAndFit(border)
        self.Fit()    # 窗体大小自适应
        self.Bind(wx.EVT_BUTTON, self.OnButtonCheck, self.buttonCheck)
        self.Bind(wx.EVT_BUTTON, self.OnButtonQuit, self.buttonQuit)

        self.panel = wx.Panel(self, -1)
        self.menuBar = wx.MenuBar()  # 创建菜单栏

        self.menu = wx.Menu()  # 创建菜单
        self.menuOpen = self.menu.Append(101, 'Open')  # 创建菜单项
        self.menuSave = self.menu.Append(102, 'Save')

        self.menu.AppendSeparator()  # 分隔符

        self.menuClose = self.menu.Append(104, 'Close')
        self.menuBar.Append(self.menu, '&File')  # 把菜单添加到菜单栏

        self.menu = wx.Menu()
        self.menuCopy = self.menu.Append(201, 'Copy')
        self.menuCut = self.menu.Append(202, 'Cut')
        self.menuBar.Append(self.menu, '&Edit')

        self.SetMenuBar(self.menuBar)
        self.Bind(wx.EVT_MENU, self.menuHandler)
    def OnButtonCheck(self, event):
        self.result.SetLabel('')
        try:
            num = int(self.primeValue.GetValue())
        except ValueError:
            self.result.SetLabel('not a integer')
            return
        n = int(math.sqrt(num))
        for i in range(2, n + 1):
            if num % i == 0:
                self.result.SetLabel('No')
                break
        else:
            self.result.SetLabel('Yes')

    def OnButtonQuit(self, event):
        dlg = wx.MessageDialog(self, 'Really Quit?', 'Caution', \
                               wx.CANCEL | wx.OK | wx.ICON_INFORMATION)
        if dlg.ShowModal() == wx.ID_OK:
            self.Destroy()

    def menuHandler(self, event):
        id = event.GetId()
        if id == 101:
            dlg = wx.MessageDialog(self, r'You click the menu item of Open', '', wx.CANCEL | wx.OK)
            if dlg.ShowModal() == wx.ID_OK:
                self.Destroy()


if __name__=='__main__':
    app=wx.App()             #创建应用程序对象
    frame=Myframe(None)      #创建框架类对象
    frame.Show(True)         #显示框架
    app.MainLoop()           #事件循环等待与处理

程序三:

import wx
class MyApp(wx.App): #这是第一步,定义了wx.App的子类;
       #编写OnInit()方法
       def OnInit(self):

           self.frame=wx.Frame(parent=None,title=u'姓名学号', size=(500, 300))
           self.panel = wx.Panel(self.frame, wx.ID_ANY)
           self.panel.SetBackgroundColour('Green')

           self.radioButtonSexM = wx.RadioButton(self.panel,-1,'Male')
           self.radioButtonSexF = wx.RadioButton(self.panel, -1, 'Female')
           self.checkBoxAdmin = wx.CheckBox(self.panel,-1,'看书')
           self.checkBoxmusic = wx.CheckBox(self.panel, -1, '音乐')
           self.checkBoxsport = wx.CheckBox(self.panel, -1, '运动')
           self.checkBoxmove = wx.CheckBox(self.panel, -1, '电影')
           self.checkBoxtravl = wx.CheckBox(self.panel, -1, '旅游')

           self.label1 = wx.StaticText(self.panel,-1,'UserName:',style=wx.ALIGN_RIGHT)
           self.label2 = wx.StaticText(self.panel, -1, 'Password:', style=wx.ALIGN_RIGHT)

           self.textName = wx.TextCtrl(self.panel,-1)
           self.textPwd = wx.TextCtrl(self.panel,-1,style=wx.TE_PASSWORD)

           self.buttonOK = wx.Button(self.panel,-1,'OK')
           self.buttomCancel = wx.Button(self.panel,-1,'Cancel')

           sizer1 = wx.FlexGridSizer(1,7,5,5)
           sizer1.AddMany(
               [(self.radioButtonSexM, 0, wx.EXPAND),
                (self.radioButtonSexF, 0, wx.EXPAND),
                (self.checkBoxAdmin, 0, wx.EXPAND),
                (self.checkBoxmusic, 0, wx.EXPAND),
                (self.checkBoxsport, 0, wx.EXPAND),
                (self.checkBoxmove, 0, wx.EXPAND),
                (self.checkBoxtravl, 0, wx.EXPAND),
                ])

           sizer2 = wx.FlexGridSizer(3,2,5,5)
           sizer2.AddMany(
               [(self.label1, 0, wx.ALIGN_LEFT),
                (self.textName, 0, wx.EXPAND),
                (self.label2, 0, wx.ALIGN_LEFT),
                (self.textPwd, 0, wx.EXPAND),
                (self.buttonOK, 0, wx.EXPAND),
                (self.buttomCancel, 0, wx.EXPAND),])
           self.border = wx.BoxSizer(wx.VERTICAL)
           self.border.Add(sizer1,0,wx.ALL,15)
           self.border.Add(sizer2, 0, wx.ALL, 15)
           self.panel.SetSizer(self.border)

           self.Bind(wx.EVT_BUTTON, self.OnButtonOK, self.buttonOK)
           self.Bind(wx.EVT_BUTTON, self.OnButtonCancel, self.buttomCancel)
           self.frame.Show()

           return True
       def OnButtonOK(self,event):
           finalStr = ''
           if self.radioButtonSexM.GetValue() == True:
               finalStr+='Sex:Male\n'
           elif self.radioButtonSexF.GetValue() == True:
               finalStr += 'Sex:Female\n'
           if self.checkBoxAdmin.GetValue() == True:
               finalStr+='看书\n'
           if self.checkBoxmusic.GetValue() == True:
               finalStr+='音乐\n'

           if self.checkBoxsport.GetValue() == True:
               finalStr += '运动\n'
           if self.checkBoxmove.GetValue() == True:
               finalStr += '电影\n'
           if self.checkBoxtravl.GetValue() == True:
               finalStr += '旅游\n'

           if self.textName.GetValue()=='wang' and self.textPwd.GetValue()=='123':
               finalStr+='用户名和密码是正确的\n'
           else:
               finalStr += '用户名和密码是不正确的\n'
           wx.MessageBox(finalStr)
       def OnButtonCancel(self,event):
           self.radioButtonSexM.SetValue(True)
           self.radioButtonSexF.SetValue(False)
           self.checkBoxAdmin.SetValue(True)
           self.checkBoxmusic.SetValue(True)
           self.checkBoxsport.SetValue(True)
           self.checkBoxmove.SetValue(True)
           self.checkBoxtravl.SetValue(True)
           self.textName.SetValue('')
           self.textPwd.SetValue('')
app = MyApp() #第三步,实例化
app.MainLoop() #第四步,调用MainLoop方法











猜你喜欢

转载自blog.csdn.net/cubejava/article/details/128480069