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

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种对话框样式。

对话框

消息框 msgbox

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

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

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

确认框 ccbox

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

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

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

布尔框 boolbox

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

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

与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.')

是否框 ynbox

ynbox(msg=’Shall I continue?’, title=’ ‘, choices=(‘[<F1>]Yes’, ‘[<F2>]No’), image=None, default_choice='[<F1>]Yes’, cancel_choice='[<F2>]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.')

选择框 choicebox

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

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

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。

整数输入框 integerbox

integerbox(msg=”, title=’ ‘, default=None, lowerbound=0, upperbound=99, image=None, root=None)

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

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

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

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

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

还有好多个对话框样式,有空再一一补上。

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2023年12月23日
下一篇 2023年12月26日

相关推荐