Python模块 | EasyGui
(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 | 使用示例
学习记录,好记不如烂笔头
什么是 EasyGUI?
- EasyGUI 是 Python 中一个非常简单的 GUI 编程模块,不同于其他的 GUI 生成器,它不是事件驱动的。相反,所有的
GUI 交互都是通过简地函数调用就可以实现 - EasyGUI 为用户提供了简单的 GUI 交互接口,不需要程序员知道任何有关 tkinter,框架,部件,回调或 lambda
的任何细节。 - EasyGUI 可以很好地兼容 Python 2 和 3,并且不存在任何依赖关系。
- EasyGUI 是运行在 Tkinter 上并拥有自身的事件循环,而 IDLE 也是 Tkinter写的一个应用程序并也拥有自身的事件循环。因此当两者同时运行的时候,有可能会发生冲突,且带来不可预测的结果。因此如果你发现你的EasyGUI 程序有这样的问题,请尝试在 IDLE 外去运行你的程序。
[EasyGui中的函数]
import easygui as m_gui
m_gui.egdemo()
- msbox
- ynbox
- ccbox
- boolbox
- buttonbox
- indexbox
- choicebox
- multchoicebox
- textbox
- codebox
- enterbox
- integerbox
- passwordbox
- multenterbox
- multpasswordbox
- filesavebox
- fileopenbox
- diropenbox
- exceptionbox
msbox | 使用示例
import easygui as m_gui
m_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 | 使用示例
import easygui as m_gui
a = m_gui.ynbox('内容', '标题', choices=('确定', '放弃'))
aa = str(a)# 验证下是不是返回的 True, 所以赋值给变量aa之前, 把变量a转换成字符串
if aa == 'True':
m_gui.msgbox('成功!!! | ynbox() 返回的是布尔类型')
else:
pass
- 设置 F1~F12 为快捷键
- 设置 a~z 为快捷键
m_gui.ynbox('设置 F1~F12 为快捷键', '标题', choices=('[<F1>]确定', '放弃'))
m_gui.ynbox('设置 a~z 为快捷键', '标题', choices=('[a]确定', '放弃'))
如果是以这种方式 [<>] 设置快捷键的话,按钮上的文字就显示该显示的,
如果是以这种方式 [] 设置快捷键的话, 按钮上会...很蠢, 也可能是我菜,没找到隐藏的方法
--------------------------------------------------------------------------------------------------------------------
ccbox | 使用示例
import easygui as m_gui
a = 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 | 使用示例
import easygui as m_gui
a = 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 | 使用示例
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 | 使用示例
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 | 使用示例
import easygui as m_gui
msg_cont = ['1.在显示前不区分大小写的方法排好序表示怀疑']# msg内容
title_cont = '标题'# title
# 关键字参数choices 的内容. 设置一些选择项,这次使用关键字参数choices 直接调用变量choices_list
choices_list = ('D', 'c', 'A', 'z', 'F')
m_gui.choicebox(msg=msg_cont, title=title_cont, choices=choices_list)
import easygui as m_gui
msg_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 | 使用示例
import easygui as m_gui
msg_cont = 'multchoicebox() 支持用户选择 0 个, 1 个或者同时选择多个选项\n也可在选择多个选项后, 取消某些已选定的选项.'
title_cont = '标题'
choices_list = [
'a1', 'a12', 'a123',
'b1', 'b12', 'b123',
'看书', '看报', '看小鸟',
'关门', '关窗', '关衣柜'
]# 关键字参数choices 的内容. 设置一些选择项,这次使用关键字参数choices 直接调用变量choices_list
new_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 | 使用示例
import easygui as m_gui
import 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 | 使用示例
- 默认返回的值会自动去除首尾的空格,如果需要保留首尾空格的话请设置参数
strip=False
import easygui as m_gui
m_guimsg = '在输入框中输入内容 !'
m_guititle = '输入消息'
a = m_gui.enterbox(m_guimsg, m_guititle)
m_gui.msgbox(msg=a, title='显示返回的内容')
integerbox | 使用示例
import easygui as m_gui
integerbox_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 | 使用示例
import easygui as m_gui
integerbox_msg = '输入密码:'
integerbox_title = '安全验证'
a = m_gui.passwordbox(integerbox_msg, integerbox_title)
m_gui.msgbox('1. 返回输入的内容: %s\n2. passwordbox() 函数返回值类型为 %s' % (a, type(a)))
multenterbox | 使用示例
- 如果用户输入的值比选项少的话, 则返回列表中的值用空字符串填充用户为输入的选项
- 返回的是个列表,如果设置了五个输入框,只填写了三个,那么返回的列表中会有五项内容,其中三项是填写的字符串内容,另外两项是未填写的空字符串
- 如果用户输入的值比选项多的话, 则返回的列表中的值将截断为选项的数量
- 参数fields 如果提供了
5
个输入框, 参数values 如果预设了6
个内容:
那么参数values 前五个内容会自动匹配到参数fields 的输入框内
参数values 的第六个内容会被忽略掉,或者说是截断掉
至于被截断,是怎么个截断法,我倒是没有仔细测验,到底是只截断最后一个?还是可以随机截断?或者可以选择性截断?
本菜B盲猜 应该可以灵活性截断,不然得话未免显得太死板,待日后技术提高了再细细测下.
- 参数values 是把输入框内容返回成一个列表的, 因此可以利用此特性做一些判断, 比如说
必选项(不能为空)
输入框中内容限制规则
等- 如果用户取消操作, 则返回域中的列表的值或者 None 值
import easygui as m_gui
multenterbox_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_gui
multenterbox_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_gui
multpasswordbox_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() 函数提供一个对话框,
让
用户选择文件需要保存的路径
(带完整路径),如果用户选择 “Cancel” 按钮则返回 None- 参数default 应该包含一个文件名(例如当前需要保存的文件名),当然也可以设置为空, 或者包含一个文件格式掩码的通配符
- 关于 filetypes 参数的设置方法:
- 可以是包含文件掩码(
或者是叫后缀名??
)的字符串列表,例如:filetypes = ["*.txt"]- 可以是字符串列表,列表的最后一项字符串是文件类型的描述,例如:filetypes = [".css", [".htm", "*.html", "HTML files"]]
- 举个例子: 打开一个文件, 然后变动下内容, 判断内容是否变动, 如果有变动则提示保存
import easygui as m_gui
import 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:
codebox_msg = '打开的文件为: %s' % (set_file_name)
codebox_title = '阅读诗词'
buttonbox_msg = '检测到文件内容有变动'
buttonbox_title = '警告!!!'
buttonbox_choices = [
'覆盖',
'另存为',
'取消操作'
]
codebox_text = f.read()
a = m_gui.codebox(msg=codebox_msg, title=codebox_title, text=codebox_text)
# [条件判断, 根据buttonbox() 函数中的按钮进行相应的处理]
if a == None:# 判断返回的是不是空值
pass
elif a[:] == codebox_text[:]:# 简单判断内容是否一样
pass
else:
buttonbox_return_choices = m_gui.buttonbox(buttonbox_msg, buttonbox_title, buttonbox_choices)
if buttonbox_return_choices == buttonbox_choices[2]:# 取消操作
pass
elif buttonbox_return_choices == None:
pass
elif buttonbox_return_choices == buttonbox_choices[1]:# 另存为
#这里拿到一个完整路径
filesavebox_save = m_gui.filesavebox(default='*.txt')
# 将新内容写入进新的路径\\文件中
with open(filesavebox_save, 'w', encoding='utf-8') as new_f:
new_f.write(a[:])
# 看下新文件的内容
with open(filesavebox_save, encoding='utf-8') as read_f:
codebox_read_f = read_f.read()
m_gui.codebox('看下新文件内容', text=codebox_read_f)
elif buttonbox_return_choices == buttonbox_choices[0]:# 覆盖
# 将新内容覆盖写入进原文件中
with open(marge_path, 'w', encoding='utf-8') as old_f:
old_f.write(a[:])
# 看下原文件的内容
with open(marge_path, encoding='utf-8') as read_f:
codebox_read_f = read_f.read()
m_gui.codebox('看下原文件内容', text=codebox_read_f)
else:
pass
1.打开文件后如果直接关闭窗口或者点击了"Cancel"按钮
,(原文件内容不会有变化也不会有新的文件被保存到硬盘上)看下原文件内容,如图:
2.如果文件内容发生变化,在选择窗口步骤中不小心关闭了窗口或者点击了"取消操作"按钮
,(原文件内容不会有变化也不会有新的文件被保存到硬盘上)看下原文件内容,如图:
3.如果文件内容发生变化,在选择窗口步骤点击了"另存为"按钮
,(原文件内容不会有变化, 新的文件会被保存在选择的完整路径下)如下图:
4.如果文件内容发生变化,在选择窗口步骤点击了"覆盖"按钮
,(原文件内容会产生变化,并被覆写为新内容且保存)如下图:
fileopenbox | 使用示例
- fileopenbox() 函数提供一个对话框,
返回
用户选择的文件名
(带完整路径),如果用户选择 “Cancel” 按钮则返回 None- 参数default 应该包含一个文件名(例如当前需要保存的文件名),当然也可以设置为空, 或者包含一个文件格式掩码的通配符
- 例如:
- default="e:\*.py" 在选择文件时显示 C:\ 盘符下所有的 Python 文件。
- default="c:\abc\test*.py" 在选择文件时显示 C:\abc 文件夹下所有的名字以 test 开头的 Python 文件
- 关于 filetypes 参数的设置方法:
- 可以是包含文件掩码(
或者是叫后缀名??
)的字符串列表,例如:filetypes = ["*.txt"]- 可以是字符串列表,列表的最后一项字符串是文件类型的描述,例如:filetypes = [".css", [".htm", "*.html", "HTML files"]]
- 参数multiple,如果为 True 则表示可以同时选择多个文件,举个例子看图:
import easygui as m_gui
m_gui.fileopenbox(msg='选择Python文件', default='e:\\*.py', multiple=True)
diropenbox | 使用示例
import easygui as m_gui
diropenbox_path = m_gui.diropenbox(msg='选择一个文件夹! ')
m_gui.msgbox('选择的路径为:%s\ndiropenbox()函数返回值的类型是: %s' % (diropenbox_path, type(diropenbox_path)), '显示路径')
Python模块 | EasyGui的更多相关文章
- Python 模块EasyGui详细介绍
转载:无知小德 Python 模块EasyGui详细介绍 EasyGui 官网: http://easygui.sourceforge.net 官方的教学文档:http://easygui-docs- ...
- 一、Python 模块EasyGui详细介绍
Python 模块EasyGui详细介绍 EasyGui 官网: -http://easygui.sourceforge.net 官方的教学文档: -easygui-docs-0.96\tutoria ...
- Python 模块EasyGui
1.msgBox msgbox(msg='(Your message goes here)', title=' ', ok_button='OK', image=None, root=None) ms ...
- python 模块-easygui.buttonbox
2018-03-0315:43:11 ): Yes_or_No = easygui.buttonbox("是否良品?", choices=['Yes', 'No', '退出']) ...
- Python模块探秘之EasyGui
在Windows想用Python开发一些简单的界面,所以找到了很容易上手的EasyGui库.下面就分享一下简单的使用吧. 参考的链接:官网Tutorial 接下来,我将从简单,到复杂一点点的演示如何使 ...
- 【Python】easygui小甲鱼
翻译改编自官方文档:http://easygui.sourceforge.net/tutorial/index.html 翻译改编者:小甲鱼,本文欢迎转载,转载请保证原文的完整性! 演示使用 Pyth ...
- 使用C/C++写Python模块
最近看开源项目时学习了一下用C/C++写python模块,顺便把学习进行一下总结,废话少说直接开始: 环境:windows.python2.78.VS2010或MingW 1 创建VC工程 (1) 打 ...
- Python模块之configpraser
Python模块之configpraser 一. configpraser简介 用于处理特定格式的文件,其本质还是利用open来操作文件. 配置文件的格式: 使用"[]"内包含 ...
- Python模块之"prettytable"
Python模块之"prettytable" 摘要: Python通过prettytable模块可以将输出内容如表格方式整齐的输出.(对于用Python操作数据库会经常用到) 1. ...
随机推荐
- Markdown入门操作
Markdown基本操作 一. 字体 1. 标题 (1). 一级标题 "# + 标题名" (2). 其余类推 (最多支持6级标题) 加粗 " ** + 内容 + ** & ...
- C语言思维导图—自己整理的
- 如何将Matlab中“模糊控制设计器”的隶属度函数导出图片(figure)
如何将Matlab中"模糊控制设计器"的隶属度函数导出图片(figure)详情参考matlab官方帮助手册:plotmf()函数https://www.mathworks.com/ ...
- 纯css模拟电子钟
先看效果 演示地址: https://yueminhu.github.io/di...点击左边拉环切换夜间模式. 用到了伪元素生成数字的小三角`currentColor和color: inherit` ...
- Linux 0.11源码阅读笔记-块设备驱动程序
块设备驱动程序 块设备驱动程序负责实现对块设备数据的读写功能.内核代码统一使用缓冲块间接和块设备(如磁盘)交换数据,缓冲区数据通过块设备驱动程序和块设备交换数据. 块设备的管理 块设备表 内核通过一张 ...
- 【Android开发】EasyPermissions 请求权限
安卓6.0以后,开发应用的时候,仅在AndroidManifest.xml中申请权限已经不可以了,需要在代码中动态申请. 现在看一个google推出的机制:EasyPermissions 引入步骤: ...
- Docker入门(windows版),利用Docker创建一个Hello World的web项目
Docker 当大家点开这篇博客的时候,相信大家对docker多多少少也有些认识了,最近学习docker这门技术,略微有些心得,写篇文章记录一下学习过程并帮大家跳过一些坑. docker的核心有两个, ...
- 「实践篇」解决微前端 single-spa 项目中 Vue 和 React 路由跳转问题
前言 本文介绍的是在做微前端 single-spa 项目过程中,遇到的 Vue 子应用和 React 子应用互相跳转路由时遇到的问题. 项目情况:single-spa 项目,基座用的是 React,目 ...
- Python入门-常用模块
1.sys,os import sys import os #获取当前的路径 print(sys.path[0]) print(os.getcwd()) print(os.path.abspath(& ...
- Spring Boot-场景启动器
分析上文快速入门 1.查看pom文件导入的依赖(starter的父项目) <parent> <artifactId>spring-boot-starter-parent< ...