ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python的GUI程序设计

2021-12-21 12:02:14  阅读:161  来源: 互联网

标签:__ Python GUI 窗体 程序设计 wx ALIGN self panel


常用GUI工具

  • Tkinter:Python的标准GUI库
  • wxPython:功能比Tkiner强,可以跨平台
  • Pyqt
  • PyObject等

wxPython安装

pip install wxPython

建立python的GUI程序三大步骤

1、导入wxPython包或其他包

2、建立框架

在这里插入图片描述

3、建立主程序

在这里插入图片描述

  • 执行FrameShow(Ture)后,框架才能看见
  • 执行App.MainLoop()后,框架才能处理事件

4、Frame

  • Frame:框架(窗体),容器,可移动、缩放,包含标题栏、菜单等。所有框架的父类。使用时,需要派生出子类,其基类构造函数格式为:
    wx.Frame.init (parent, id, title, pos, size, style, name )
  • parent:框架的父窗体。
  • id:新窗体的wxPython ID号。可以明确地传递一个,也可传递-1,这时wxPython将自动生成一个新的ID。
  • title:窗体的标题
  • pos:wx.Point对象,它指定这个新窗体的左上角在屏幕中的位置。通常(0,0)是显示器的左上角。当将其设定为wx.DefaultPosition,其值为(-1,-1),表示让系统决定窗体的位置。
  • size:一个wx.Size对象,它指定这个新窗体的初始尺寸。当将其设定为wx.DefaultSize时,其值为(-1,-1),表示由系统决定窗体的初始尺寸。
  • style:指定窗体的类型。
  • name:框架的名字。
  • 当一个参数前面的所有参数都被设定的时候,该参数可以省略其名称直接填写其值,否则需要使用 “参数名=值”的格式。

wx.Frame的常用样式:

wx.CAPTION:增加标题栏
wx.DEFAULT_FRAME_STYLE:默认样式
wx.CLOSE_BOX:标题栏上显示“关闭”按钮
wx.MAXIMIZE_BOX:标题栏上显示“最大化”按钮
wx.MINIMIZE_BOX:标题栏上显示“最小化”按钮
wx.RESIZE_BORDER:边框可改变尺寸
wx.SIMPLE_BORDER:边框没有装饰
wx.SYSTEM_MENU:增加系统菜单(有“关闭”、“移动”、“改变尺寸”等功能)
wx.FRAME_SHAPED:用该样式创建的框架可以使用SetShape()方法来创建一个非矩形的窗体
wx.FRAME_TOOL_WINDOW:给框架一个比正常小的标题栏,使框架看起来像一个工具框窗体

TextCtrl的style

wx.TE_CENTER:控件中的文本居中。
wx.TE_LEFT:控件中的文本左对齐。默认行为。
wx.TE_NOHIDESEL:文本始终高亮显示,只适用于Windows。
wx.TE_PASSWORD:不显示所键入的文本,代替以星号显示。
wx.TE_READONLY:文本控件为只读,用户不能修改其中的文本。
wx.TE_RIGHT:控件中的文本右对齐。
wx.TE_MULTILINE: 多行文本控件
重要方法:
使用GetValue()方法获取文本框中输入的内容,使用SetValue()方法设置文本框中的文本

实验内容

实验一

编写代码,通过选择单选按钮,将对应信息写入第一个TextCtrl (FrameMouse右侧)当中。如选择FrameSize,则在第一个TextCtrl 显示窗体宽和高,当选择FramePos时,则在第一个TextCtrl 显示窗体的x和y坐标位置。通过选择多选按钮AllInfo,则能在第二个多行TextCtrl中显示窗体和鼠标的全部三项信息。
实验图片

# -*- coding: utf-8 -*-
"""
Created on 2021-12-21 上午 10:54

@author: 浅笑醉红楼.(3303295829@qq.com)

@Software: PyCharm

(1) create by 浅笑醉红楼. 2021-12-21 上午 10:54
"""
import wx

class TestCtrlFrame(wx.Frame):
    def __init__(self,superion):
        wx.Frame.__init__(self, parent=superion, title='My Frist Form')
        self.panel = wx.Panel(self)
        self.radioButtonFramePos = wx.RadioButton(self.panel, -1, 'FramePos:')
        self.radioButtonFrameSize = wx.RadioButton(self.panel, -1, 'FrameSize:')
        self.radioButtonMousePos = wx.RadioButton(self.panel, -1, 'MousePos:')
        self.checkBoxAllinfo = wx.CheckBox(self.panel, -1, 'AllInfo')

        self.label1 = wx.StaticText(self.panel, -1, 'FrameMouse', style=wx.ALIGN_LEFT)
        self.label2 = wx.StaticText(self.panel, -1, 'AllInfo', style=wx.ALIGN_LEFT)

        self.FrameMouse = wx.TextCtrl(self.panel, -1, "", style=wx.TE_READONLY)
        self.AllInfo = wx.TextCtrl(self.panel, -1, "", style=wx.TE_MULTILINE,size=(150,100))

        sizer1 = wx.FlexGridSizer(4, 2, 5, 5)
        sizer1.AddMany(
            [(self.radioButtonFramePos, 0, wx.EXPAND),
             (self.radioButtonFrameSize, 0, wx.EXPAND),
             (self.radioButtonMousePos, 0, wx.EXPAND),
             (self.checkBoxAllinfo, 0, wx.EXPAND),
             (self.label1, 0, wx.ALIGN_RIGHT),
             (self.FrameMouse, 0, wx.SHAPED),
             (self.label2, 0, wx.ALIGN_RIGHT),
             (self.AllInfo, 0, wx.SHAPED)])

        self.border = wx.BoxSizer(wx.VERTICAL)
        self.border.Add(sizer1, 0, wx.ALL, 80)
        self.panel.SetSizerAndFit(self.border)
        self.Fit()  # 窗体大小自适应

        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Bind(wx.EVT_MOVE, self.OnFrameMove)
        self.panel.Bind(wx.EVT_MOTION, self.OnMouseMove)

    def OnSize(self, event):
        self.size = event.GetSize()
        if self.radioButtonFrameSize.GetValue() == True:
            self.FrameMouse.SetValue("%s, %s" % (self.size.width, self.size.height))
        if self.checkBoxAllinfo.GetValue() == True:
            self.AllInfo.SetValue("窗体大小%s, %s\n窗体位置%s, %s\n鼠标位置%s, %s" % (self.size.width, self.size.height,self.posFrameMove.x, self.posFrameMove.y,self.pos.x, self.pos.y))

    def OnFrameMove(self,event):
        self.posFrameMove = event.GetPosition()
        if self.radioButtonFramePos.GetValue() == True:
            self.FrameMouse.SetValue("%s, %s" % (self.posFrameMove.x, self.posFrameMove.y))
        if self.checkBoxAllinfo.GetValue() == True:
            self.AllInfo.SetValue("窗体大小%s, %s\n窗体位置%s, %s\n鼠标位置%s, %s" % (self.size.width, self.size.height,self.posFrameMove.x, self.posFrameMove.y,self.pos.x, self.pos.y))

    def onm ouseMove(self, event):  # 鼠标移动事件处理函数
        self.pos = event.GetPosition()
        if self.radioButtonMousePos.GetValue() == True:
            self.FrameMouse.SetValue("%s, %s" % (self.pos.x, self.pos.y))
        if self.checkBoxAllinfo.GetValue() == True:
            self.AllInfo.SetValue("窗体大小%s, %s\n窗体位置%s, %s\n鼠标位置%s, %s" % (self.size.width, self.size.height,self.posFrameMove.x, self.posFrameMove.y,self.pos.x, self.pos.y))


if __name__ == '__main__':
    app=wx.App()
    frame = TestCtrlFrame(None)
    frame.Show()
    app.MainLoop()



实验二

编写代码选择单选钮、复选框并输入文本框中要求的用户名和密码之后单击“OK”按钮会弹出消息框提示输入和选择的内容,并打开聊天窗体;单击“Cancel”按钮自动清除用户的输入,并默认将单选钮“Male”设置为选中状态。
在这里插入图片描述

# -*- coding: utf-8 -*-
"""
Created on 2021-12-21 上午 10:45

@author: 浅笑醉红楼.(3303295829@qq.com)

@Software: PyCharm

(1) create by 浅笑醉红楼. 2021-12-21 上午 10:45
"""
import wx
from CH9_GUI import ChatWND 

class Chat(wx.Frame):
    def __init__(self,superion):
        wx.Frame.__init__(self, parent=superion, title='wx GUI')
        self.panel = wx.Panel(self, 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, 'Aministrator')
        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.buttonCancel = wx.Button(self.panel, -1, 'Cancel')
        sizer1 = wx.FlexGridSizer(1, 3, 5, 5)
        sizer1.AddMany(
            [(self.radioButtonSexM, 0, wx.EXPAND),
             (self.radioButtonSexF, 0, wx.EXPAND),
             (self.checkBoxAdmin, 0, wx.EXPAND), ])

        sizer2 = wx.FlexGridSizer(3, 2, 5, 5)
        sizer2.AddMany(
            [(self.label1, 0, wx.EXPAND),
             (self.textName, 0, wx.EXPAND),
             (self.label2, 0, wx.EXPAND),
             (self.textPwd, 0, wx.EXPAND),
             (self.buttonOK, 0, wx.EXPAND),
             (self.buttonCancel, 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.SetSizerAndFit(self.border)
        self.Fit()  # 窗体大小自适应

        self.Bind(wx.EVT_BUTTON, self.OnButtonOK, self.buttonOK)
        self.Bind(wx.EVT_BUTTON, self.OnButtonCancel, self.buttonCancel)

    # 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 += 'Administrator\n'
    #     if self.textName.GetValue() == 'yang' and self.textPwd.GetValue() == '123':
    #         finalStr += 'user name and password are correct\n'
    #     else:
    #         finalStr += 'user name or password is incorrect\n'
    #     wx.MessageBox(finalStr)

    def OnButtonCancel(self, event):
        self.radioButtonSexM.SetValue(True)
        self.radioButtonSexF.SetValue(False)
        self.checkBoxAdmin.SetValue(True)
        self.textName.SetValue('')
        self.textPwd.SetValue('')

    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 += 'Administrator\n'
        if self.textName.GetValue() == 'yang' and self.textPwd.GetValue() == '123':
            finalStr += 'user name and password are correct\n'
            wx.MessageBox(finalStr)
            frame = ChatWND.ChatWND(None)  # 创建框架类对象
            frame.Show(True)
            self.Destroy()
        else:
            finalStr += 'user name or password is incorrect\n'
            wx.MessageBox(finalStr)

if __name__ == '__main__':
    app=wx.App()
    frame = Chat(None)
    frame.Show()
    app.MainLoop()

聊天界面设计(ChatWND.py)

# -*- coding: utf-8 -*-
"""
Created on 2021-12-21 上午 10:54

@author: 浅笑醉红楼.(3303295829@qq.com)

@Software: PyCharm

(1) create by 浅笑醉红楼. 2021-12-21 上午 10:54
"""
import wx

class ChatWND(wx.Frame):
    def __init__(self, superior):
        wx.Frame.__init__(self, parent=superior,title=u'Chat Window', size=(800, 600))
        self.panel = wx.Panel(self, wx.ID_ANY)
        self.panel.SetBackgroundColour("Green")

        self.label1 = wx.StaticText(self.panel, -1, 'IP:', style=wx.ALIGN_LEFT)
        self.label2 = wx.StaticText(self.panel, -1, 'PORT:', style=wx.ALIGN_RIGHT)
        self.label3 = wx.StaticText(self.panel, -1, 'MSG:', style=wx.ALIGN_LEFT, size=(50, 50))
        self.label4 = wx.StaticText(self.panel, -1, 'Send:', style=wx.ALIGN_LEFT, size=(50, 50))

        self.textIP = wx.TextCtrl(self.panel, -1)
        self.textPORT = wx.TextCtrl(self.panel, -1)
        self.textRecord = wx.TextCtrl(self.panel, -1, style=wx.TE_MULTILINE, size=(520, 200))
        self.textSend = wx.TextCtrl(self.panel, -1, style=wx.TE_MULTILINE, size=(520, 30))

        self.buttonConnectSvr = wx.Button(self.panel, -1, 'Connect')
        self.buttonSendMsg = wx.Button(self.panel, -1, 'Send')
        self.buttonGetFiles = wx.Button(self.panel, -1, 'File')

        sizer1 = wx.FlexGridSizer(2, 2, 20, 15)
        sizer1.AddMany(
            [
                (self.label3, 0, wx.ALIGN_LEFT),
                (self.textRecord, 0, wx.ALIGN_RIGHT),
                (self.label4, 0, wx.ALIGN_LEFT),
                (self.textSend, 0, wx.ALIGN_RIGHT),

            ])
        sizer2 = wx.FlexGridSizer(1, 7, 5, 5)
        sizer2.AddMany(
            [(self.label1, 0, wx.ALIGN_LEFT),
             (self.textIP, 0, wx.EXPAND),
             (self.label2, 0, wx.ALIGN_LEFT),
             (self.textPORT, 0, wx.ALIGN_RIGHT),
             (self.buttonConnectSvr, 0, wx.EXPAND),
             (self.buttonSendMsg, 0, wx.EXPAND),
             (self.buttonGetFiles, 0, wx.ALIGN_RIGHT)
             ])
        self.border = wx.BoxSizer(wx.VERTICAL)
        self.border.Add(sizer1, 0, wx.ALL, 15)
        self.border.Add(sizer2, 0, wx.ALL, 15)
        self.panel.SetSizerAndFit(self.border)
        self.Fit()  # 窗体大小自适应
if __name__ == '__main__':
    app=wx.App()
    frame = ChatWND(None)
    frame.Show()
    app.MainLoop()

标签:__,Python,GUI,窗体,程序设计,wx,ALIGN,self,panel
来源: https://blog.csdn.net/qq_52827563/article/details/122058580

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有