Python模块探秘之EasyGui

本文详细介绍EasyGUI库的基本使用方法,包括消息框、按钮选择、文本输入等组件的示例代码,适合初学者快速掌握在Windows环境下使用Python进行简单界面开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       

在Windows想用Python开发一些简单的界面,所以找到了很容易上手的EasyGui库。下面就分享一下简单的使用吧。

参考的链接:官网Tutorial



接下来,我将从简单,到复杂一点点的演示如何使用这个模块。希望能给刚接触easygui的你一点帮助 :-)


msgBox,ccbox,ynbox

# coding:utf-8#    __author__ = 'Mark sinoberg'#    __date__ = '2016/5/25'#    __Desc__ =  一个最简单的类似于Java的MessageBox的小窗口import easyguititle = easygui.msgbox(msg='提示信息',title='标题部分',ok_button="OOK")msg = easygui.msgbox('Hello Easy GUI')print '返回值:' + msgccbox = easygui.ccbox("here is Continue | Cancel Box!")print '返回值:' + str(ccbox)ynbox = easygui.ynbox("Yes Or No Button Box!")print '返回值: ' + str(ynbox)
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

bottonbox

# coding:utf-8#    __author__ = 'Mark sinoberg'#    __date__ = '2016/5/25'#    __Desc__ = 能让你最初选择的简单的界面,第二个参数为一个列表import easygui# choice = easygui.buttonbox("这里是提示的语句信息:\n", title='三选一', choices=['one' \#     , 'two', 'three'])# easygui.msgbox('您选择了:' + str(choice))## # choices 内只能有两个参数 ,选择哪一个将返回1,否则返回0# bool = easygui.boolbox('msg提示信息', title='标题部分', choices=['A', 'B'])# easygui.msgbox(bool)image = 'me.jpg'msg = 'Here is my photo,a python fan also'choices = ['Yes','No',"Not Sure"]title = 'Am I handsome?'easygui.buttonbox(msg,title,image=image,choices=choices)
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

choicebox

# coding:utf-8#    __author__ = 'Mark sinoberg'#    __date__ = '2016/5/25'#    __Desc__ = 从一个列表中选择其中的一个,会有返回值的出现import easyguimsg = '选择此列表项中你喜欢的一个吧'title = '必须选择一个哦'choices = ['1','2','3','4','5','6','7']answer = easygui.choicebox(msg,title,choices)print '你选择了 :' + str(answer)
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

enterbox

# coding:utf-8#    __author__ = 'Mark sinoberg'#    __date__ = '2016/5/25'#    __Desc__ = 可以满足用户输入的控件import easyguist = easygui.enterbox("请输入一段文字:\n")print "您输入了:  " + str(st)
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

mutilchoicebox

# coding:utf-8#    __author__ = 'Mark sinoberg'#    __date__ = '2016/5/25'#    __Desc__ = 一个多选的列表项.呵呵了,这个版本貌似有问题。我的多选并没有真正的实现import easyguimsg = '选择此列表项中你喜欢的一个吧'title = '必须选择一个哦'choices = (1,2,3,4,5,6,7,8,9)answer1 = easygui.multchoicebox(msg,title,choices)for item in answer1:    print item
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

intenterbox,passenterbox

# coding:utf-8#    __author__ = 'Mark sinoberg'#    __date__ = '2016/5/25'#    __Desc__ = 提供给用户简单的输入框,只能是给定的数字的范围import easyguimsg = '请输入一个数字,范围在0-100'title = '限制为数字类型'lowerbound = 0upperbound = 100default = ''image = 'me.jpg'result = easygui.integerbox(msg,title,default,lowerbound,upperbound,image)print result
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

textbox,codebox

# coding:utf-8#    __author__ = 'Mark sinoberg'#    __date__ = '2016/5/25'#    __Desc__ = easygui 还提供了对大量文本的支持,以及对代码文本的支持import easyguimsg = '大文本的支持'title = 'Text Code'text = 'abcdefghijklmnopqrstuvwxyzABCDEFGHJIKLMNOPQRSTUVWXYZ0123456789-/'textContent = easygui.textbox(msg,title,text)codeContent = easygui.codebox(msg,title,)print textContentprint codeContent# D:\Software\Python2\python.exe E:/Code/Python/MyTestSet/easygui_/text_codebox.py# abcdefghijklmnopqrstuvwxyzABCDEFGHJIKLMNOPQRSTUVWXYZ0123456789-/# public class HelloWorld{#   public static void main(String []args) {#       System.out.println("Hello World!");#   }# }## Process finished with exit code 0
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

diropenbox

# coding:utf-8#    __author__ = 'Mark sinoberg'#    __date__ = '2016/5/25'#    __Desc__ = 该函数用于提供一个对话框,返回用户选择的目录名,该目录名是带有完整的路径的# 选择Cancel的话返回值默认为Noneimport easyguimsg = '选择一个文件,将会返回该文件的完整的目录哦'title = ' 文件选择对话框'default = r'F:\flappy-bird'full_file_path = easygui.diropenbox(msg, title, default)print '选择的文件的完整的路径为:' + str(full_file_path)# D:\Software\Python2\python.exe E:/Code/Python/MyTestSet/easygui_/diropenbox.py# 选择的文件的完整的路径为:F:\flappy-bird## Process finished with exit code 0
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

fileopenbox

# coding:utf-8#    __author__ = 'Mark sinoberg'#    __date__ = '2016/5/25'#    __Desc__ = 此方法用于提供一个对话框,返回用户选择的文件名,带有完整的路径,选择Cancel返回None#              default="c:/fishc/*.py" 即显示 C:\fishc 文件夹下所有的 Python 文件。#              default="c:/fishc/test*.py" 即显示 C:\fishc 文件夹下所有的名字以 test 开头的 Python 文件。#              filetypes参数是包含文件掩码的字符串的列表,记住是个列表。如:filetypes = ["*.css", ["*.htm", "*.html", "HTML files"]]import easyguimsg = '返回选择的文件的完整的路径,选择Cancel则返回None'title = '文件选择器'default = 'E:/Code/Python/MyTestSet/easygui/*.py'opened_files = easygui.fileopenbox(msg,title,default,multiple=True)for item in opened_files:    print item# D:\Software\Python2\python.exe E:/Code/Python/MyTestSet/easygui_/fileopenbox.py# E:\Code\Python\MyTestSet\easygui_\me.jpg# E:\Code\Python\MyTestSet\easygui_\buttonbox.py# E:\Code\Python\MyTestSet\easygui_\choicesbox.py# E:\Code\Python\MyTestSet\easygui_\diropenbox.py# E:\Code\Python\MyTestSet\easygui_\enterbox.py# E:\Code\Python\MyTestSet\easygui_\fileopenbox.py# E:\Code\Python\MyTestSet\easygui_\integerbox.py## Process finished with exit code 0
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

filesavebox

# coding:utf-8#    __author__ = 'Mark sinoberg'#    __date__ = '2016/5/25'#    __Desc__ = 该函数提供了一个对话框,让用户选择文件需要保存的路径(带完整的路径)选择Cancel返回None#               default 参数应该包含一个文件名(例如当前需要保存的文件名),当然你也可以设置为空的,或者包含一个文件格式掩码的通配符。#               filetypes参考如上面的fileopenboximport easyguimsg = 'Save your file'title = "to Save File"default = 'E:/Code/Python/MyTestSet/easygui/newFile.*'savedfile = easygui.filesavebox(msg,title,default)print savedfileprint '当然了,这里仅仅是一个完整的路径加上文件名而已,并不会真的保存成一个文件,保存文件需要用到其他的库'# D:\Software\Python2\python.exe E:/Code/Python/MyTestSet/easygui_/filesavebox.py# E:\Code\Python\MyTestSet\easygui_\newFile.doc# 当然了,这里仅仅是一个完整的路径加上文件名而已,并不会真的保存成一个文件,保存文件需要用到其他的库## Process finished with exit code 0
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

exceptionbox

# coding:utf-8#    __author__ = 'Mark sinoberg'#    __date__ = '2016/5/25'#    __Desc__ = 这是一个很好用的对话框,当应用程序出现异常的时候,就可以通过这个来给与用户友好的界面提示import easyguitry:    int('Exception')except:    easygui.exceptionbox('int类型数据转换错误!请检查您的数据类型!')# 会弹出一个界面,内容信息可以自己定义,如上面。下面的内容就是追踪到的出错信息# Traceback (most recent call last):#   File "E:/Code/Python/MyTestSet/easygui_/exceptionbox.py", line 10, in <module>#     int('Exception')# ValueError: invalid literal for int() with base 10: 'Exception'
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

总结


看完了这些示例,想必对easygui开发简单的桌面小程序很有信心了吧。(^__^) 嘻嘻……

但是咧,对于比较复杂的任务,只是掌握了这些基础的是远远不够的。所以我们还需要挖掘一下Python其他的相关的模块。这样在实际开发的时候,就可以根据任务的难易程度选择最合适的模块进行开发了。

           

给我老师的人工智能教程打call!http://blog.youkuaiyun.com/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值