当前位置:Gxlcms > Python > Python模块EasyGui详细介绍

Python模块EasyGui详细介绍

时间:2021-07-01 10:21:17 帮助过:19人阅读

Python 模块EasyGui详细介绍

前言:

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

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

msgBox,ccbox,ynbox

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

bottonbox

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

choicebox

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

enterbox

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

mutilchoicebox

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

intenterbox,passenterbox

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

textbox,codebox

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

diropenbox

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

fileopenbox

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

filesavebox

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

exceptionbox

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

总结

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

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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多Python 模块EasyGui详细介绍相关文章请关注PHP中文网!

人气教程排行