(Python模块 | EasyGui | 2021/04/08)
目录
- 什么是 EasyGUI?
- [EasyGui中的函数]
- msbox | 使用示例
- ynbox | 使用示例
- ccbox | 使用示例
- boolbox | 使用示例
- buttonbox | 使用示例
- indexbox | 使用示例
- choicebox | 使用示例
- multchoicebox | 使用示例
- textbox | 使用示例
- codebox | 使用示例
- enterbox | 使用示例
- integerbox | 使用示例
- passwordbox | 使用示例
- multenterbox | 使用示例
- multpasswordbox | 使用示例
- filesavebox | 使用示例
- fileopenbox | 使用示例
- diropenbox | 使用示例
学习记录,好记不如烂笔头
- 如有不对之处欢迎大佬指点 !
- 学习文章来源<点击访问>
- 记录平台 :
- Github
- Blog[大灰狼]
- HelloFlask 论坛( 官网 ) ( 镜像 )
- (代码写的很一般,轻喷..我目前还是初学者-未入门)
- EasyGUI 是 Python 中一个非常简单的 GUI 编程模块 , 不同于其他的 GUI 生成器 , 它不是事件驱动的 。相反 , 所有的
GUI 交互都是通过简地函数调用就可以实现 - EasyGUI 为用户提供了简单的 GUI 交互接口 , 不需要程序员知道任何有关 tkinter , 框架 , 部件 , 回调或 lambda
的任何细节 。 - EasyGUI 可以很好地兼容 Python 2 和 3 , 并且不存在任何依赖关系 。
- EasyGUI 是运行在 Tkinter 上并拥有自身的事件循环 , 而 IDLE 也是 Tkinter写的一个应用程序并也拥有自身的事件循环 。因此当两者同时运行的时候 , 有可能会发生冲突 , 且带来不可预测的结果 。因此如果你发现你的EasyGUI 程序有这样的问题 , 请尝试在 IDLE 外去运行你的程序 。
import easygui as m_guim_gui.egdemo()- msbox
- ynbox
- ccbox
- boolbox
- buttonbox
- indexbox
- choicebox
- multchoicebox
- textbox
- codebox
- enterbox
- integerbox
- passwordbox
- multenterbox
- multpasswordbox
- filesavebox
- fileopenbox
- diropenbox
- exceptionbox
msgbox(msg='(Your message goes here)', title=' ', ok_button='OK', image=None, root=None)
import easygui as m_guim_gui.msgbox('dhl')# 仅使用了msg参数m_gui.msgbox('dhl', '标题')# 使用了msg参数| title参数m_gui.msgbox('dhl', '标题', ok_button='我是按钮')# 使用了msg参数| title参数 | 变更按钮文字######## 函数: msgbox() 会返回一个True ########a = m_gui.msgbox('函数: msgbox() 会返回一个True')if a:m_gui.msgbox('成功!!!')else:pass
文章插图

文章插图

文章插图

文章插图

文章插图
ynbox | 使用示例ynbox() 函数的默认语法如下: (参数:title 看情况可用可不用)
ynbox(msg='Shall I continue?', title=' ', choices=('Continue', 'Cancel'), image=None)
参数:choices 提供了两个按钮(返回值为布尔类型), 按钮: Continue返回True | 按钮: Cancel返回False
import easygui as m_guia = m_gui.ynbox('内容', '标题', choices=('确定', '放弃'))aa = str(a)# 验证下是不是返回的 True, 所以赋值给变量aa之前, 把变量a转换成字符串if aa == 'True':m_gui.msgbox('成功!!! | ynbox() 返回的是布尔类型')else:pass
文章插图

文章插图
【python模块库大全官网 Python模块 | EasyGui】参数:choices 还可以设定快捷键
- 设置 F1~F12 为快捷键
- 设置 a~z 为快捷键
m_gui.ynbox('设置 F1~F12 为快捷键', '标题', choices=('[<F1>]确定', '放弃'))
文章插图
m_gui.ynbox('设置 a~z 为快捷键', '标题', choices=('[a]确定', '放弃'))
文章插图
如果是以这种方式 [<>] 设置快捷键的话,按钮上的文字就显示该显示的,
如果是以这种方式 [] 设置快捷键的话, 按钮上会...很蠢, 也可能是我菜,没找到隐藏的方法
--------------------------------------------------------------------------------------------------------------------
ccbox | 使用示例ccbox() 函数的默认语法等同于ynbox() 函数
参数:choices 的快捷键用法也是等同于ynbox() 函数
参数:choices 返回值也是为布尔类型, 不过写这篇笔记前看了网上相关的文章说ccbox() 函数是返回的为整数 1或0, 实测不对 !
举个例子如下:
import easygui as m_guia = m_gui.ccbox('内容', '标题', choices=('[<F1>]确定', '[<F2>]放弃'))aa = str(a)# 验证下是返回的<布尔>还是<整数>, 把变量a转换成字符串if aa == 'True':m_gui.msgbox('成功!!! | ccbox() 返回的是布尔类型')elif aa == '1':m_gui.msgbox('成功!!! | ccbox() 返回整数型 1 或 0')else:pass
文章插图

文章插图
boolbox | 使用示例boolbox () 函数的默认语法如下: (参数:title 看情况可用可不用)
boolbox(msg='Shall I continue?', title=' ', choices=('Continue', 'Cancel'), image=None, default_choice='Yes', cancel_choice='No')
参数:choices 提供了两个按钮(返回值为布尔类型), 按钮: Continue返回True | 按钮: Cancel返回False
import easygui as m_guia = m_gui.boolbox('内容', '标题', choices=('[<F1>]确定', '[<F2>]放弃'))aa = str(a)# 把变量a转换成字符串# 根据转换后的值,验证下是返回的<布尔>还是<整数> if aa == 'True':m_gui.msgbox('成功!!! | boolbox() 返回的是布尔类型')elif aa == '1':m_gui.msgbox('成功!!! | boolbox() 返回整数型 1 或 0')else:pass
文章插图

文章插图
buttonbox | 使用示例buttonbox() 函数的默认语法如下: (参数:title 看情况可用可不用)
buttonbox(msg='', title=' ', choices=('Button[1]', 'Button[2]', 'Button[3]'), image=None, images=None, default_choice=None, cancel_choice=None, callback=None, run=True)
当用户点击任意一个按钮的时候 , buttonbox() 函数返回按钮的文本内容
- 如果按钮中设置了快捷键, 那么快捷键这个字符串的内容也会被返回
import easygui as m_gui#设置个按钮列表btn_list = ['[<F1>]按钮','[<F2>]2','[<F3>]3','Python','[d])()*&Y^^%^%\\re123']a = m_gui.buttonbox('内容', '标题', choices=(btn_list[0], btn_list[1], btn_list[2], btn_list[3], btn_list[4]))for aa in btn_list:# 临时变量aa 在btn_list中取数据if aa == a:# 如果某个数据等于变量a, 这个变量a 也就是buttonbox() 函数返回按钮的文本内容m_gui.msgbox('buttonbox的返回内容: %s' % aa, '显示返回内容')
文章插图

文章插图

文章插图

文章插图

文章插图

文章插图
indexbox | 使用示例indexbox() 函数的默认语法如下: (参数:title 看情况可用可不用)
indexbox(msg='Shall I continue?', title=' ', choices=('Buttone1', 'Buttone2'), image=None, default_choice='Yes', cancel_choice='No')
基本用法跟buttonbox() 一样, 区别在于indexbox() 函数的参数:choices 返回的是 0 1 2 3 4 5 这种,第一个按钮就返回0,第二个按钮返回1
import easygui as m_gui#设置个按钮列表btn_list = ['[<F1>]Button_1','[<F2>]Button_2','[<F3>]Button_3']a = m_gui.indexbox('内容', '标题', choices=(btn_list[0], btn_list[1], btn_list[2]))for aa in range(len(btn_list)):if aa == a and a == int(a):aa += 1# Python从0开始的, 我们人是从1开始数的m_gui.msgbox('选择的是第 %d 个按钮\nindexbox() 函数返回的值是int类型' % aa)
文章插图

文章插图
choicebox | 使用示例choicebox() 函数的默认语法如下: (参数:title 看情况可用可不用)
choicebox(msg='Pick an item', title='', choices=[], preselect=0, callback=None, run=True)
choicebox() 为用户提供了一个可选择的列表 , 使用序列(元祖或列表)作为选项 , 这些选项显示前会按照不区分大小写的方法排好序
- 举些例子:
- 1.在显示前不区分大小写的方法排好序表示怀疑, 因为代码运行后并没有看到选择项里的内容有排序
- 2.另外还可以使用键盘来选择其中一个选项(可多次点击选择 | 如果是以字母或数字开头的话), 顺便看下choicebox() 函数返回的类型
import easygui as m_guimsg_cont = ['1.在显示前不区分大小写的方法排好序表示怀疑']# msg内容title_cont = '标题'# title# 关键字参数choices 的内容. 设置一些选择项,这次使用关键字参数choices 直接调用变量choices_listchoices_list = ('D', 'c', 'A', 'z', 'F')m_gui.choicebox(msg=msg_cont, title=title_cont, choices=choices_list)
文章插图
import easygui as m_guimsg_cont = '2.另外还可以使用键盘来选择其中一个选项(可多次点击选择 | 如果是以字母或数字开头的话)\n顺便看下choicebox() 函数返回的类型'title_cont = '标题'choices_list = ['a1', 'a12', 'a123','b1', 'b12', 'b123','看书', '看报', '看小鸟','关门', '关窗', '关衣柜']a = m_gui.choicebox(msg=msg_cont, title=title_cont, choices=choices_list)#a = m_gui.choicebox(msg_cont, title_cont, choices_list)# 也可以这样写,相当于是去掉了关键字参数for aa in range(len(choices_list)):if choices_list[aa] == a:aa += 1# Python从0开始的, 我们人是从1开始数的m_gui.msgbox('选择的是第 %d 个选项\nchoicebox() 函数返回值类型是%s' % (aa, type(a)))
文章插图

文章插图

文章插图
multchoicebox | 使用示例multchoicebox() 函数的默认语法如下: (参数:title 看情况可用可不用)
multchoicebox(msg='Pick an item', title='', choices=[], preselect=0, callback=None, run=True)
multchoicebox() 函数也是提供一个可选择的列表, 与choicebox() 不同的是:
- multchoicebox() 支持用户选择 0 个, 1 个或者同时选择多个选项, 也可在选择多个选项后, 取消某些已选定的选项.
- multchoicebox() 函数的返回值是一个列表
- 双击列表中的选项返回空列表 , 必须通过“OK”按钮选择(这句话其实并不完全正确, 就是在测试操作中这种表达不绝对)
import easygui as m_guimsg_cont = 'multchoicebox() 支持用户选择 0 个, 1 个或者同时选择多个选项\n也可在选择多个选项后, 取消某些已选定的选项.'title_cont = '标题'choices_list = ['a1', 'a12', 'a123','b1', 'b12', 'b123','看书', '看报', '看小鸟','关门', '关窗', '关衣柜']# 关键字参数choices 的内容. 设置一些选择项,这次使用关键字参数choices 直接调用变量choices_listnew_choices_list = []# 设一个空列表,存放multchoicebox() 函数返回的序列(或者说是列表?)a = m_gui.multchoicebox(msg=msg_cont, title=title_cont, choices=choices_list)#a = m_gui.choicebox(cont_list, title_cont, choices_list)# 也可以这样写,相当于是去掉了关键字参数new_choices_list.extend(a)# 在列表末尾一次性追加[a]序列中的多个值m_gui.msgbox('选中的内容如下: \n%s' % new_choices_list[:])
文章插图

文章插图

文章插图
textbox | 使用示例textbox() 函数的默认语法如下: (参数:title 看情况可用可不用)
textbox(msg=' ', title=' ', text='', codebox=False, callback=None, run=True)
textbox() 函数默认会以比例字体(参数 codebox=True 设置为等宽字体)来显示文本内容(自动换行), 这个函数适合用于显示一般的书面文字 。
- 参数text 设置可编辑文本区域的内容 , 可以是字符串、列表或者元祖类型 。
- 参数 codebox=True 设置为等宽字体
- textbox() 函数有两个按钮[Cancel]和[OK]
- 点击[Cancel]按钮返回None
- 点击[OK]按钮返回文本区域显示的内容,
如果修改了文本区域的内容,则返回修改过的内容
import easygui as m_guiimport os as m_os# 指定目录路径, 指定需要打开的文件名, 改变工作目录至路径set_file_path = 'E:\\'set_file_name = '诗词.txt'm_os.chdir(set_file_path)#拼接路径和文件名得到完整路径 | 调用路径并打开文件marge_path = m_os.path.join(m_os.getcwd(), set_file_name)with open(marge_path, encoding='utf-8') as f:m_guimsg = '打开的文件为: %s' % (set_file_name)m_guititle = '阅读诗词'm_guitext = f.read()a = m_gui.textbox(msg=m_guimsg, title=m_guititle, text=m_guitext, codebox=True)# 将textbox() 函数返回的内容赋值给变量a, 然后用msgbox() 函数看下返回的什么m_gui.msgbox(a, '我是<msgbox> | 看下返回内容')
文章插图

文章插图
codebox | 使用示例codebox() 函数的默认语法如下: (参数:title 看情况可用可不用)
codebox(msg='', title=' ', text='')
codebox() 函数以等宽字体显示文本内容(不自动换行), 相当于 textbox(codebox=True), 不做演示示例-感觉没啥用
直接参考textbox() 函数的示例enterbox | 使用示例enterbox() 函数的默认语法如下: (参数:title 看情况可用可不用)
enterbox(msg='Enter something.', title='', default='', strip=True, image=None, root=None)
enterbox() 函数会为用户提供一个最简单的输入框, 返回值为用户在输入框中输入的内容,
类型[str]- 默认返回的值会自动去除首尾的空格 , 如果需要保留首尾空格的话请设置参数
strip=False
import easygui as m_guim_guimsg = '在输入框中输入内容 !'m_guititle = '输入消息'a = m_gui.enterbox(m_guimsg, m_guititle)m_gui.msgbox(msg=a, title='显示返回的内容')
文章插图

文章插图
integerbox | 使用示例integerbox() 函数的默认语法如下: (参数:title 看情况可用可不用)
integerbox(msg='', title=' ', default=None, lowerbound=0, upperbound=99, image=None, root=None)
integerbox() 函数为用户提供一个简单的输入框, 用户只能输入范围内(参数lowerbound设置最小值 , 参数upperbound设置最大值)的整型数值, 否则会要求用户重新输入
- 假设用户准备输入的数字为X, 设定 lowerbound=0, upperbound=10 | 那么用户只能输入介于 0 ≤ X ≤ 10 之间的单个数字
import easygui as m_guiintegerbox_msg = '只能输入 0 ≤ x ≤ 10 之间的数字, 包含0和10'integerbox_title = '输入数字'a = m_gui.integerbox(integerbox_msg, integerbox_title, lowerbound=0, upperbound=10)m_gui.msgbox('1. 返回输入的数字 %d\n2. integerbox() 函数返回值类型为 %s' % (a, type(a)))
文章插图

文章插图
passwordbox | 使用示例passwordbox() 函数的默认语法如下: (参数:title 看情况可用可不用)
passwordbox(msg='Enter your password.', title=' ', default='', image=None, root=None)
passwordbox() 函数跟 enterbox() 函数用法一样, 不同的是用户输入的内容用星号 (*) 显示出来, 该函数返回用户输入的字符串
import easygui as m_guiintegerbox_msg = '输入密码:'integerbox_title = '安全验证'a = m_gui.passwordbox(integerbox_msg, integerbox_title)m_gui.msgbox('1. 返回输入的内容: %s\n2. passwordbox() 函数返回值类型为 %s' % (a, type(a)))
文章插图

文章插图
multenterbox | 使用示例multenterbox() 函数的默认语法如下: (参数:title 看情况可用可不用)
multenterbox(msg='Fill in values for the fields.', title=' ', fields=[], values=[], callback=None, run=True)
multenterbox() 函数为用户提供多个简单的输入框 , 要注意以下几点 :
- 如果用户输入的值比选项少的话, 则返回列表中的值用空字符串填充用户为输入的选项
- 返回的是个列表,如果设置了五个输入框,只填写了三个,那么返回的列表中会有五项内容,其中三项是填写的字符串内容,另外两项是未填写的空字符串
- 如果用户输入的值比选项多的话, 则返回的列表中的值将截断为选项的数量
- 参数fields 如果提供了
5个输入框, 参数values 如果预设了6个内容:那么参数values 前五个内容会自动匹配到参数fields 的输入框内参数values 的第六个内容会被忽略掉,或者说是截断掉至于被截断,是怎么个截断法,我倒是没有仔细测验,到底是只截断最后一个?还是可以随机截断?或者可以选择性截断?本菜B盲猜 应该可以灵活性截断,不然得话未免显得太死板,待日后技术提高了再细细测下.
- 参数values 是把输入框内容返回成一个列表的, 因此可以利用此特性做一些判断, 比如说
必选项(不能为空)输入框中内容限制规则等
- 参数fields 如果提供了
- 如果用户取消操作, 则返回域中的列表的值或者 None 值
import easygui as m_guimultenterbox_msg = '例子: 不预设参数values'multenterbox_title = '资料填写'multenterbox_fields = ['姓名: ', '性别: ', '联系电话: ', '联系邮箱: ', '填写推荐码: ']a = m_gui.multenterbox(multenterbox_msg, multenterbox_title, fields=multenterbox_fields)m_gui.msgbox('1. 返回输入的内容: %s\n2. multenterbox() 函数返回值类型为 %s' % (a, type(a)))
文章插图

文章插图
?
import easygui as m_guimultenterbox_msg = '例子: 预设了参数values, 其中有六个内容'multenterbox_title = '资料填写'multenterbox_fields = ['姓名: ', '性别: ', '联系电话: ', '联系邮箱: ', '填写推荐码: ']multenterbox_values = ['1', '2', '3', '4', '5', '6']a = m_gui.multenterbox(multenterbox_msg, multenterbox_title, fields=multenterbox_fields, values=multenterbox_values)m_gui.msgbox('1. 返回输入的内容: %s\n2. multenterbox() 函数返回值类型为 %s' % (a, type(a)))
文章插图

文章插图
multpasswordbox | 使用示例multpasswordbox() 函数的默认语法如下: (参数:title 看情况可用可不用)
multpasswordbox(msg='Fill in values for the fields.', title=' ', fields=(), values=(), callback=None, run=True)
使用方法上可以说是跟
multenterbox() 函数一样, 唯一的区别就是multpasswordbox() 函数提供的最后一个输入框显示为密码的形式(*)import easygui as m_guimultpasswordbox_msg = '例子:multpasswordbox() 函数最后一个输入框显示为密码的形式(*)'multpasswordbox_title = '找回帐号'multpasswordbox_fields = ['姓名: ', '性别: ', '联系电话: ', '联系邮箱: ', '超级密码: ']a = m_gui.multpasswordbox(multpasswordbox_msg, multpasswordbox_title, fields=multpasswordbox_fields)m_gui.msgbox('1. 返回输入的内容: %s\n2. multpasswordbox() 函数返回值类型为 %s' % (a, type(a)))
文章插图

文章插图
filesavebox | 使用示例filesavebox() 函数的默认语法如下: (参数:title 看情况可用可不用)
filesavebox(msg=None, title=None, default='', filetypes=None)
- filesavebox() 函数提供一个对话框 ,
让用户选择文件需要保存的路径(带完整路径) , 如果用户选择 “Cancel” 按钮则返回 None - 参数default 应该包含一个文件名(例如当前需要保存的文件名) , 当然也可以设置为空, 或者包含一个文件格式掩码的通配符
- 关于 filetypes 参数的设置方法:
- 可以是包含文件掩码(
或者是叫后缀名??)的字符串列表 , 例如:filetypes = ["*.txt"] - 可以是字符串列表 , 列表的最后一项字符串是文件类型的描述 , 例如:filetypes = ["
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
- 可以是包含文件掩码(
