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

一、图形用户界面(GUI)

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

>>> import easygui
>>> easygui.egdemo() #easygui模块中自带的一个演示功能,easygui.egdemo会自动跳出一个图形界面演示里面的功能

输出

![image.png](https://upload-images.jianshu.io/upload_images/15631448-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://upload-images.jianshu.io/upload_images/15631448-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://upload-images.jianshu.io/upload_images/15631448-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://upload-images.jianshu.io/upload_images/15631448-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://upload-images.jianshu.io/upload_images/15631448-99b1e6790d45ae7c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![image.png](https://upload-images.jianshu.io/upload_images/15631448-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://upload-images.jianshu.io/upload_images/15631448-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://upload-images.jianshu.io/upload_images/15631448-0b9c950920ff3381.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![image.png](https://upload-images.jianshu.io/upload_images/15631448-562ef39a8e8bd223.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


![image.png](https://upload-images.jianshu.io/upload_images/15631448-9666c190a126fed4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![image.png](https://upload-images.jianshu.io/upload_images/15631448-d05fee1a224fd7bf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 11,338评论 0 10
  • 1 GUI对象层次结构 2 GUI的基本知识 本小节主要介绍如何新建GUI,打开GUI文件、Layout编辑器,G...
    Yuki_kau阅读 13,206评论 0 6
  • Python 面向对象Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对...
    顺毛阅读 9,731评论 4 16
  • 1、图形化界面设计的基本理解当前流行的计算机桌面应用程序大多数为图形化用户界面(Graphic User Inte...
    IIronMan阅读 259,624评论 15 244
  • 你认为那些应该跟你同病相怜的人,才是会出卖你的人;那些跟你一样在这里受折磨的人,是首先折磨你的人。让犹太人把犹太人...
    摄影师柳丁阅读 1,670评论 0 1

友情链接更多精彩内容