美文网首页
基本扩展模块/简单图形界面

基本扩展模块/简单图形界面

作者: 无尽的结 | 来源:发表于2019-01-20 15:13 被阅读0次

    一、图形用户界面(GUI)

    1.GUI是人机交互的图形化界面设计,包括展示数据用的小控件、输入的方法、菜单、按钮以及窗口等 ,用户通过鼠标、键盘等输入设备操纵屏幕上的图标或菜单选项,来执行选择命令、调用文件、启动程序等日常任务
    2.easygui模块:可以显示各种对话框、文本框、选择框与用户交互功能演示如下图:
    
    >>> import easygui
    >>> easygui.egdemo() #easygui模块中自带的一个演示功能,easygui.egdemo会自动跳出一个图形界面演示里面的功能
    

    输出

    ![image.png](https://img.haomeiwen.com/i15631448/cc227d07422716be.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    3.easygui模块常用函数

    3.1 msgbox()函数/消息窗口详情如下图所示:

    >>> import easygui
    >>> easygui.msgbox(msg='(Your message goes here)',title=' ',ok_button='ok',image=None,root=None)
    #显示一条信息和提供一个ok按钮,用户可指定任意的消息和标题,甚至重写'ok'按钮的内容比如改为'确定',当弹出这么个消息窗口后如果用户不点击'ok'按钮,程序则会一直停留在这个界面,是一个高度可自定化的消息窗口
    

    输出

    ![image.png](https://img.haomeiwen.com/i15631448/2853cbec9472060f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    

    3.2 choicebox()函数/按钮选项,可以生成一个复选下拉框的一个窗口如下图:

    >>> import easygui
    >>> easygui.choicebox(msg='pick an item',title=' ',choices=['choices1','choices2','choices3','...'])
    #这里的选项可以用元组或者列表都可以,例子中使用的是列表作为选项的内容
    

    输出

    ![image.png](https://img.haomeiwen.com/i15631448/51403c878ecd94a4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    

    3.3 textbox()函数/显示文本内容,参数可以是字符串、列表、或者元组类型。如下图:

    >>> import easygui
    >>> easygui.textbox(msg='show text message',title=' ',text='''Beautiful is better than ugly.explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.''',codebox=0)
    #前面的msgbox()函数显示的区域有限只能显示一行而textbox()函数则可以显示多行内容
    

    输出

    ![image.png](https://img.haomeiwen.com/i15631448/1356fd5a040c68d2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    

    3.4 passwordbox()函数/输入密码,类似于enterbox(),但是用户输入的内容是用'*'号显示出来.如下图所示:

    >>> import easygui
    >>> easygui.passwordbox(msg='Enter your password.',title=' ',default='',image=None,root=None)
    #这里的default=''的意思是用户必须要用鼠标明确的按窗口的'ok'按钮或者'cancel'
    

    输出

    ![](https://img.haomeiwen.com/i15631448/99b1e6790d45ae7c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    ![image.png](https://img.haomeiwen.com/i15631448/085be54da850fd4b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    

    3.5 fileopenbox()函数/打开文件,调用easygui.fileopenbox它会弹出一个系统级的选择文件的对话框,在对话框中就可以选择一个文件点ok,fileopenbox就会返回该文件的全路径如下图:

    >>> import easygui
    >>> easygui.fileopenbox()
    'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\tcl\\tcl8.6\\clock.tcl'
    

    输出

    ![image.png](https://img.haomeiwen.com/i15631448/f99b58862abed807.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    #在对话框中选一个文件,就会返回该文件的全路径
    
    4设置一个简单的图形界面演示小程序如下图:
    >>> import easygui as g #首先引用easygui这个模块并重命名为g
    >>> import sys #引用sys模块
    >>> while True:  #反复执行这个程序
        g.msgbox('hello,欢迎进入第一个GUI制作的小游戏~') #首先显示'hello,欢迎进入第一个GUI制作的小游戏~这条消息
        msg = '你希望学习到什么知识呢?'#对话框显示这条信息并让你继续选择
        title = '互动小游戏'#标题为互动小游戏
        choices = ['琴棋书画','四书五经','程序编写','逆向分析']#选项内容
        choice = g.choicebox(msg,title,choices)
        g.msgbox('你的选择是:'+str(choice),'结果')显示用户选择的结果
        msg = '你希望重新开始小游戏吗?'#弹出对话框让用户继续选择是否开始游戏
        title = '请选择'#标题为请选择
        if g.ccbox(msg,title): #引用ccbox这个函数弹出一个对话框让用户选择continue/cancel按钮,如果用户选择continue则程序回到开始重新执行,选择cancel则只直接执行else下面的语句直接退出。
            pass
        else:
            sys.exit(0)
    

    输出

    ![image.png](https://img.haomeiwen.com/i15631448/0b9c950920ff3381.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    ![image.png](https://img.haomeiwen.com/i15631448/562ef39a8e8bd223.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    
    ![image.png](https://img.haomeiwen.com/i15631448/9666c190a126fed4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    ![image.png](https://img.haomeiwen.com/i15631448/d05fee1a224fd7bf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    

    相关文章

      网友评论

          本文标题:基本扩展模块/简单图形界面

          本文链接:https://www.haomeiwen.com/subject/oygbdqtx.html