Python开发中,系统默认的IO交互形式过于单一,所以找到了一个比较容易上手的EasyGui库。这个库是基于Tkinter封装的,所以要保证环境支持Tkinter。虽然很丑,但也勉强能用。本文介绍了不同的弹窗类型,并对常用方法附加示例代码,供参考。

安装:pip install easygui -i https://pypi.tuna.tsinghua.edu.cn/simple

1、msgbox()

功能:消息弹窗

msgbox(msg="(Your message goes here)", title=" ", ok_button="OK", image=None, root=None)

参数:

  • msg: 要展示的内容
  • title: 弹窗标题
  • ok_button: 确认按钮文字
  • image: 图片

返回值:

按钮文字(字符串类型)

示例:

import easygui
#只显示文字
easygui.msgbox('提示信息')
#显示文字和图片
easygui.msgbox('提示信息', image='./bg.png')
#显示文字,并修改窗口标题、按钮文字
easygui.msgbox('提示信息', title='弹窗标题', ok_button='确认')

2、enterbox()

功能:文本输入框

enterbox(msg="Enter something.", title=" ", default="", strip=True, image=None, root=None)

参数:

  • msg: 要展示的内容
  • title: 弹窗标题
  • default: 默认值
  • strip: True/False 是否清除两端空格
  • image: 图片

返回值:

用户输入内容(字符串类型)

示例:

import easygui
# 带提示文字
text = easygui.enterbox('请输入内容:')

3、indexbox()

功能:按钮选择弹窗

indexbox(msg="Shall I continue?", title=" ", choices=("Yes", "No"), image=None, default_choice='Yes', cancel_choice='No')

参数:

  • msg: 要展示的内容
  • title: 弹窗标题
  • choices: 按钮选项
  • image: 图片
  • default_choice: 确认按钮文字
  • cancel_choice: 取消按钮文字

返回值:

用户选择的按钮索引值(字符串类型)

示例:

import easygui
#显示提示和按钮选项
result = easygui.indexbox('请选择:', choices=['java', 'python', 'php', 'go'])

4、multenterbox()

功能:多行文本输入框

multenterbox(msg="Fill in values for the fields.", title=" ", fields=[], values=[], callback=None, run=True)

参数:

  • msg: 要展示的内容
  • title: 弹窗标题
  • fields: 字段名
  • values: 默认值
  • callback: 回调函数

返回值:

用户输入的内容(列表类型)

示例:

import easygui
result = easygui.multenterbox(msg="请输入以下信息:", fields = ["姓名","年龄","电话"])
# 多行文本框,带默认值
result = easygui.multenterbox(msg="请输入以下信息:", fields = ["姓名","年龄","电话"], values = ["张三", "20", "17600208909"])

5、multchoicebox()

功能:选择列表弹窗(可多选)

multchoicebox(msg="Pick an item", title="", choices=None, preselect=0, callback=None, run=True)

参数:

  • msg: 要展示的内容
  • title: 弹窗标题
  • choices: 选项文字
  • preselect: 默认选中
  • callback: 回调函数

返回值:

用户选择的内容(列表类型)

示例:

import easygui
result = easygui.multchoicebox('请选择你爱吃哪些水果?', choices=["西瓜","香蕉","苹果","梨"])

6、ynbox()

功能:确认弹窗,可选择“是”或者“否”

easygui.ynbox(msg="Shall I continue?", title=" ", choices=("[<F1>]Yes", "[<F2>]No"), image=None, default_choice='[<F1>]Yes', cancel_choice='[<F2>]No')

参数:

  • msg: 提示文字
  • title: 弹窗标题
  • choices: 按钮选项
  • image: 图片
  • default_choice: 确认按钮文字
  • cancel_choice: 取消按钮文字

返回值:

True / False

示例:

import easygui
# 修改按钮文字
result = easygui.ynbox('还要继续吗?', choices=("确认", "取消"))

7、fileopenbox()

功能:选择本地文件

fileopenbox(msg=None, title=None, default='*', filetypes=None, multiple=False)

参数:

  • msg: 提示文字
  • title: 弹窗标题
  • default: 默认打开路径
  • filetypes: 文件类型
  • multiple: 是否可以多选

返回值:

选中文件绝对路径(字符串或列表)

示例:

import easygui
res = easygui.fileopenbox('请选择文件:')

其他弹窗方法

1、ccbox()

确认弹窗,用法和参数同 ynbox()

ccbox(msg="Shall I continue?", title=" ", choices=("C[o]ntinue", "C[a]ncel"), image=None, default_choice='Continue', cancel_choice='Cancel')

2、boolbox()

确认弹窗,用法和参数同 ynbox()

boolbox(msg="Shall I continue?", title=" ", choices=("[T]rue", "[F]alse"), image=None, default_choice='[T]rue',  cancel_choice='[F]alse')

3、integerbox()

整数输入弹窗,用法和参数同 enterbox(),用户只能输入 lowerbound 到 upperbound 之间的整数

integerbox(msg="", title=" ", default=None, lowerbound=0, upperbound=99, image=None, root=None)

4、passwordbox()

密码弹窗,用法和参数同 enterbox(),用户输入的内容用"*"显示出来

passwordbox(msg="Enter your password.", title=" ", default="", image=None, root=None)

5、buttonbox()

按钮选择弹窗,用法和参数同 indexbox(),返回用户选择的按钮文字

buttonbox(msg="", title=" ", choices=("Button[1]", "Button[2]", "Button[3]"), image=None, images=None, default_choice=None, cancel_choice=None, callback=None, run=True)

6、choicebox()

选择列表弹窗(单选),用法和参数同 multchoicebox(),这个只能单选

choicebox(msg="Pick an item", title="", choices=None, preselect=0, callback=None, run=True)

7、multpasswordbox()

多行密码弹窗,用法和参数同 multenterbox(),最后一个输入框显示为密码的形式("*")

multpasswordbox(msg="Fill in values for the fields.", title=" ", fields=tuple(), values=tuple(), callback=None, run=True)

8、textbox()

多行文本输入框,codebox=1 表示等宽字体

textbox(msg='', title=' ', text='', codebox=0)

9、codebox()

代码输入框

codebox(msg="", title=" ", text="")

10、exceptionbox()

异常显示弹窗

exceptionbox(msg=None, title=None)

11、diropenbox()

目录选择弹窗

diropenbox(msg=None, title=None, default=None)

12、filesavebox()

选择的文件需要保存的路径

filesavebox(msg=None, title=None, default="", filetypes=None)

本文为 陈华 原创,欢迎转载,但请注明出处:http://www.ichenhua.cn/read/204