Python简易图形界面库easygui对话框整理大全

 更新时间:2024年01月11日 09:58:10   作者:Hann Yang  
这篇文章主要给大家介绍了关于Python简易图形界面库easygui对话框的相关资料,EasyGUI是一个用Python编写的非常简易的GUI编程模块,文中通过代码介绍的非常详细,需要的朋友可以参考下

easygui

安装

C:\> pip install easygui

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting easygui
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/8e/a7/b276ff776533b423710a285c8168b52551cb2ab0855443131fdc7fd8c16f/easygui-0.98.3-py2.py3-none-any.whl (92 kB)
Installing collected packages: easygui
Successfully installed easygui-0.98.3

导入

>>> import easygui 
>>> easygui.__all__

['buttonbox', 'diropenbox', 'fileopenbox', 'filesavebox', 'textbox', 'ynbox', 'ccbox', 'boolbox', 'indexbox', 'msgbox', 'integerbox', 'multenterbox', 'enterbox', 'exceptionbox', 'choicebox', 'codebox', 'passwordbox', 'multpasswordbox', 'multchoicebox', 'EgStore', 'eg_version', 'egversion', 'abouteasygui', 'egdemo']

由以上列表,可以看到easygui共包含了19种对话框样式。

对话框

1. 消息框 msgbox

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

    The ``msgbox()`` function displays a text message and offers an OK button. The message text appears in the center of the window, the title text appears in the title bar, and you can replace the "OK" default text on the button.
    :param str msg: the msg to be displayed
    :param str title: the window title
    :param str ok_button: text to show in the button
    :param str image: Filename of image to display
    :param tk_widget root: Top-level Tk widget

    :return: the text of the ok_button

显示文本消息并提供“确定”按钮。消息文本显示在窗口的中心,标题文本显示在标题栏中,可以替换按钮上的“确定”默认文本,例如:

easygui.msgbox("备份完成!", title="结束", ok_button="干得好!")

2. 确认框 ccbox

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

The ``ccbox()`` function offers a choice of Continue and Cancel, and returns either True (for continue) or False (for cancel).
    :param str msg: the msg to be displayed
    :param str title: the window title
    :param list choices: a list or tuple of the choices to be displayed
    :param str image: Filename of image to display
    :param str default_choice: The choice you want highlighted when the gui appears
    :param str cancel_choice: If the user presses the 'X' close, which button should be pressed

    :return: True if 'Continue' or dialog is cancelled, False if 'Cancel'

提供了“继续”和“取消”选项,并返回True(表示继续)或False(表示取消)。默认的按钮文本为:'Continue' 和 'Cancel',也可以使用按钮文本自定义,例如: 

easygui.ccbox(msg, title, choices=('退出[E]','取消[C]'))

3. 布尔框 boolbox

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

    The ``boolbox()`` (boolean box) displays two buttons. Returns returns ``True`` if the first button is chosen. Otherwise returns ``False``.

    :param str msg: The message shown in the center of the dialog window.
    :param str title: The window title text.
    :param list choices: A list or tuple of strings for the buttons' text.
    :param str image: The filename of an image to display in the dialog window.
    :param str default_choice: The text of the default selected button.
    :param str cancel_choice: If the user presses the 'X' close, which button should be pressed

    :return: `True` if first button pressed or dialog is cancelled, `False` if second button is pressed.

如果选择了第一个按钮,则返回“True”。否则返回“False”。

与msgbox的联用,代码如下: 

import easygui
message = "What do they say?"
title = "Romantic Question"
if easygui.boolbox(message, title, ["They love me", "They love me not"]):
    easygui.msgbox('You should send them flowers.')
else:
    easygui.msgbox('It was not meant to be.')

4. 是否框 ynbox

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

    :param msg: the msg to be displayed
    :type msg: str
    :param str title: the window title
    :param list choices: a list or tuple of the choices to be displayed
    :param str image: Filename of image to display
    :param str default_choice: The choice you want highlighted when the gui appears
    :param str cancel_choice: If the user presses the 'X' close, which button should be pressed

    :return: True if 'Yes' or dialog is cancelled, False if 'No'

提供了Yes和No的选择,并返回“True”或“False”。

import easygui
result = easygui.ynbox('Is a hot dog a sandwich?', 'Hot Dog Question')
if result == True:
    easygui.msgbox('That is an interesting answer.')
else:
    easygui.msgbox('Well, that is your opinion.')

5. 选择框 choicebox

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

    The ``choicebox()`` provides a list of choices in a list box to choose from. The choices are specified in a sequence (a tuple or a list).

    :param str msg: the msg to be displayed
    :param str title: the window title
    :param list choices: a list or tuple of the choices to be displayed
    :param preselect: Which item, if any are preselected when dialog appears

    :return: A string of the selected choice or None if cancelled

在列表框中提供了可供选择的由元组或列表指定的选项列表。

import easygui
msg ="What is your favorite flavor?"
title = "Ice Cream Survey"
choices = ["Vanilla", "Chocolate", "Strawberry", "Coffee Latte"]
choice = easygui.choicebox(msg, title, choices)  # choice is a string
print(choice)

注:选择“Chocolate”后点OK就把所选择的项赋值给变量choice,点Cancel则返回None。

6. 整数输入框 integerbox

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

    Show a box in which a user can enter an integer.
    In addition to arguments for msg and title, this function accepts integer arguments for "default", "lowerbound", and "upperbound".
    The default, lowerbound, or upperbound may be None.
    When the user enters some text, the text is checked to verify that it can be converted to an integer between the lowerbound and upperbound.
    If it can be, the integer (not the text) is returned.
    If it cannot, then an error msg is displayed, and the integerbox is redisplayed.
    If the user cancels the operation, None is returned.

    :param str msg: the msg to be displayed
    :param str title: the window title
    :param int default: The default value to return
    :param int lowerbound: The lower-most value allowed
    :param int upperbound: The upper-most value allowed
    :param str image: Filename of image to display
    :param tk_widget root: Top-level Tk widget
    :return: the integer value entered by the user

显示一个框,用户可以在其中输入整数。除了msg和title的参数外,此函数还接受“default”、“lowerbound”和“upperfound”的整数参数。默认值、下限值或上限值可能为“None”。

当用户输入一些文本时,会检查文本以验证它是否可以转换为介于下限和上限之间的整数。

  • 如果可以,则返回整数(而不是文本)。
  • 如果不能,则会显示一条错误消息,并重新显示integebox。
  • 如果用户取消操作,则返回None。

import easygui
result = easygui.integerbox('请输入一个整数:')
print(result)

注:输入整数超出上下限或输入的不是一个整数,返回一个msgbox:

7. 按钮选择框 buttonbox

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

    Display a message, a title, an image, and a set of buttons.
    The buttons are defined by the members of the choices argument.

    :param str msg: the msg to be displayed
    :param str title: the window title
    :param list choices: a list or tuple of the choices to be displayed
    :param str image: (Only here for backward compatibility)
    :param str images: Filename of image or iterable or iteratable of iterable to display
    :param str default_choice: The choice you want highlighted when the gui appears

    :return: the text of the button that the user selected

显示多个按钮,按钮由参数choices的元组来定义,按钮的个数取决于元组的元素个数。

import easygui as eg
eg.buttonbox(msg='请选择:', title='自定义确认框', choices=('浏览...', '确定', '取消'), image=None, images=None, default_choice="确定", cancel_choice=None, callback=None, run=True)

8. 单行文本框 enterbox

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

    Show a box in which a user can enter some text.
    You may optionally specify some default text, which will appear in the nterbox when it is displayed.

    :param str msg: the msg to be displayed.
    :param str title: the window title
    :param str default: value returned if user does not change it
    :param bool strip: If True, the return value will have its whitespace stripped before being returned
    :return: the text that the user entered, or None if they cancel the operation.

显示一个框,用户可以在其中输入一些文本。您可以选择指定一些默认文本显示时显示在对话框中,如下图:

import easygui as eg
reply = eg.enterbox('请输入车牌号:','单行文本框', default='例如:苏ENH905')
if reply:
    eg.msgbox(f'你的输入为:{reply}')
else:
    eg.msgbox('输入为空,或者点了取消Cancel')

9. 多行文本框 textbox

textbox(msg='', title=' ', text='', codebox=False, callback=None, run=True)

Displays a dialog box with a large, multi-line text box, and returns the entered text as a string. The message text is displayed in a proportional font and wraps.

    Parameters
    ----------
    msg : string
        text displayed in the message area (instructions...)
    title : str
        the window title
    text: str, list or tuple
        text displayed in textAreas (editable)
    codebox: bool
        if True, don't wrap and width is set to 80 chars
    callback: function
        if set, this function will be called when OK is pressed
    run: bool
        if True, a box object will be created and returned, but not run

    Returns
    -------
    None
        If cancel is pressed
    str
        If OK is pressed returns the contents of textArea

 显示一个带有多行文本框的对话框,并将输入的文本作为字符串返回。例如:

import easygui as eg
txt = '''一、基本信息
  姓名:XX
  性别:X
  年龄:X
  婚姻状况:XX
  毕业院校:XX
  联系电话:XXXXXXXXXXX
二、求职意向
  意向岗位:XXXX
  证书:XXX
  薪资要求:面议
  工作能力及专长:本人有一定的......。
三、工作经历
  XXXX年X月~XXXX年X月,......。
  XXXX年X月~XXXX年X月,......。
四、自我评价
  本人对工作......'''
reply = eg.textbox(msg='请按以下模板输入你的简历:', title='简历', text=txt, codebox=False, callback=None, run=True)
 
print(reply)

总结

到此这篇关于Python简易图形界面库easygui对话框的文章就介绍到这了,更多相关Python图形界面库easygui对话框内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python编程根据字典列表相同键的值进行合并

    Python编程根据字典列表相同键的值进行合并

    这篇文章主要介绍了来学习Python字典列表根据相同键的值进行合并的操作方法,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2021-10-10
  • Python+Matplotlib绘制重叠柱状图

    Python+Matplotlib绘制重叠柱状图

    重叠柱状图 (Overlapped Bar) 是一种比较图,可以将两个柱状图叠加在一起,显示两个相关变量之间的差异,所以本文就来用Matplotlib绘制一个简单的重叠柱状图吧
    2023-06-06
  • 浅谈django三种缓存模式的使用及注意点

    浅谈django三种缓存模式的使用及注意点

    这篇文章主要介绍了浅谈django 三种缓存模式的使用及注意点,详细的介绍了三种缓存模式,非常具有实用价值,需要的朋友可以参考下
    2018-09-09
  • Pycharm出现卡顿、反应慢及CPU占用高等问题解决

    Pycharm出现卡顿、反应慢及CPU占用高等问题解决

    相信很多刚开始使用pycharm不太熟练的小伙伴,每天一开机打开pycharm总是卡半天,不知道的还以为是电脑卡了或者啥问题的,下面这篇文章主要给大家介绍了关于Pycharm出现卡顿、反应慢及CPU占用高等问题解决的相关资料,需要的朋友可以参考下
    2023-06-06
  • Matplotlib绘图基础之文本标注详解

    Matplotlib绘图基础之文本标注详解

    Matplotlib 文本和标注可以为数据和图形之间提供额外的信息,帮助观察者更好地理解数据和图形的含义,下面就将通过示例依次介绍文本和标注的常用使用方式
    2023-08-08
  • 基于django 的orm中非主键自增的实现方式

    基于django 的orm中非主键自增的实现方式

    这篇文章主要介绍了基于django 的orm中非主键自增的实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • python 链接sqlserver 写接口实例

    python 链接sqlserver 写接口实例

    这篇文章主要介绍了python 链接sqlserver 写接口实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • python中JWT用户认证的实现

    python中JWT用户认证的实现

    这篇文章主要介绍了python中JWT用户认证的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Python 自动化修改word的案例

    Python 自动化修改word的案例

    这篇文章主要介绍了Python 自动化修改word的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • Python链式调用数据处理实际应用实例探究

    Python链式调用数据处理实际应用实例探究

    本文将深入介绍Python链式调用的概念、原理以及实际应用,通过丰富的示例代码,帮助读者更全面地理解和应用这一编程技巧
    2024-01-01

最新评论