(Python模块 | EasyGui | 2021/04/08)


学习记录,好记不如烂笔头

什么是 EasyGUI?

  1. EasyGUI 是 Python 中一个非常简单的 GUI 编程模块,不同于其他的 GUI 生成器,它不是事件驱动的。相反,所有的

    GUI 交互都是通过简地函数调用就可以实现
  2. EasyGUI 为用户提供了简单的 GUI 交互接口,不需要程序员知道任何有关 tkinter,框架,部件,回调或 lambda

    的任何细节。
  3. EasyGUI 可以很好地兼容 Python 2 和 3,并且不存在任何依赖关系。
  4. EasyGUI 是运行在 Tkinter 上并拥有自身的事件循环,而 IDLE 也是 Tkinter写的一个应用程序并也拥有自身的事件循环。因此当两者同时运行的时候,有可能会发生冲突,且带来不可预测的结果。因此如果你发现你的EasyGUI 程序有这样的问题,请尝试在 IDLE 外去运行你的程序。

[EasyGui中的函数]

通过如下方式可得知EasyGui中有19种函数方法:

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 | 使用示例

msgbox() 函数的默认语法如下: (参数:title 看情况可用可不用)

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

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 | 使用示例

ynbox() 函数的默认语法如下: (参数:title 看情况可用可不用)

ynbox(msg='Shall I continue?', title=' ', choices=('Continue', 'Cancel'), image=None)

参数:choices 提供了两个按钮(返回值为布尔类型), 按钮: Continue返回True | 按钮: Cancel返回False

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


参数: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_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 | 使用示例

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_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 | 使用示例

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() 为用户提供了一个可选择的列表,使用序列(元祖或列表)作为选项,这些选项显示前会按照不区分大小写的方法排好序

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 | 使用示例

multchoicebox() 函数的默认语法如下: (参数:title 看情况可用可不用)

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

multchoicebox() 函数也是提供一个可选择的列表, 与choicebox() 不同的是:

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 | 使用示例

textbox() 函数的默认语法如下: (参数:title 看情况可用可不用)

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

textbox() 函数默认会以比例字体(参数 codebox=True 设置为等宽字体)来显示文本内容(自动换行), 这个函数适合用于显示一般的书面文字。

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 | 使用示例

enterbox() 函数的默认语法如下: (参数:title 看情况可用可不用)

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

enterbox() 函数会为用户提供一个最简单的输入框, 返回值为用户在输入框中输入的内容,类型[str]

import easygui as m_gui

m_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设置最大值)的整型数值, 否则会要求用户重新输入

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 | 使用示例

passwordbox() 函数的默认语法如下: (参数:title 看情况可用可不用)

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

passwordbox() 函数跟 enterbox() 函数用法一样, 不同的是用户输入的内容用星号 (*) 显示出来, 该函数返回用户输入的字符串

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 | 使用示例

multenterbox() 函数的默认语法如下: (参数:title 看情况可用可不用)

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

multenterbox() 函数为用户提供多个简单的输入框,要注意以下几点 :

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() 函数的默认语法如下: (参数:title 看情况可用可不用)

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

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() 函数的默认语法如下: (参数:title 看情况可用可不用)用法上和filesavebox() 函数差不多

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

import easygui as m_gui

m_gui.fileopenbox(msg='选择Python文件', default='e:\\*.py', multiple=True)


diropenbox | 使用示例

diropenbox() 函数的默认语法如下: (参数:title 看情况可用可不用)

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

diropenbox() 函数用于提供一个对话框,返回用户选择的目录名(带完整路径), 如果用户选择 “Cancel” 则返回 None

import easygui as m_gui

diropenbox_path = m_gui.diropenbox(msg='选择一个文件夹! ')
m_gui.msgbox('选择的路径为:%s\ndiropenbox()函数返回值的类型是: %s' % (diropenbox_path, type(diropenbox_path)), '显示路径')



Python模块 | EasyGui的更多相关文章

  1. Python 模块EasyGui详细介绍

    转载:无知小德 Python 模块EasyGui详细介绍 EasyGui 官网: http://easygui.sourceforge.net 官方的教学文档:http://easygui-docs- ...

  2. 一、Python 模块EasyGui详细介绍

    Python 模块EasyGui详细介绍 EasyGui 官网: -http://easygui.sourceforge.net 官方的教学文档: -easygui-docs-0.96\tutoria ...

  3. Python 模块EasyGui

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

  4. python 模块-easygui.buttonbox

    2018-03-0315:43:11 ): Yes_or_No = easygui.buttonbox("是否良品?", choices=['Yes', 'No', '退出']) ...

  5. Python模块探秘之EasyGui

    在Windows想用Python开发一些简单的界面,所以找到了很容易上手的EasyGui库.下面就分享一下简单的使用吧. 参考的链接:官网Tutorial 接下来,我将从简单,到复杂一点点的演示如何使 ...

  6. 【Python】easygui小甲鱼

    翻译改编自官方文档:http://easygui.sourceforge.net/tutorial/index.html 翻译改编者:小甲鱼,本文欢迎转载,转载请保证原文的完整性! 演示使用 Pyth ...

  7. 使用C/C++写Python模块

    最近看开源项目时学习了一下用C/C++写python模块,顺便把学习进行一下总结,废话少说直接开始: 环境:windows.python2.78.VS2010或MingW 1 创建VC工程 (1) 打 ...

  8. Python模块之configpraser

    Python模块之configpraser   一. configpraser简介 用于处理特定格式的文件,其本质还是利用open来操作文件. 配置文件的格式: 使用"[]"内包含 ...

  9. Python模块之"prettytable"

    Python模块之"prettytable" 摘要: Python通过prettytable模块可以将输出内容如表格方式整齐的输出.(对于用Python操作数据库会经常用到) 1. ...

随机推荐

  1. Markdown入门操作

    Markdown基本操作 一. 字体 1. 标题 (1). 一级标题 "# + 标题名" (2). 其余类推 (最多支持6级标题) 加粗 " ** + 内容 + ** & ...

  2. C语言思维导图—自己整理的

  3. 如何将Matlab中“模糊控制设计器”的隶属度函数导出图片(figure)

    如何将Matlab中"模糊控制设计器"的隶属度函数导出图片(figure)详情参考matlab官方帮助手册:plotmf()函数https://www.mathworks.com/ ...

  4. 纯css模拟电子钟

    先看效果 演示地址: https://yueminhu.github.io/di...点击左边拉环切换夜间模式. 用到了伪元素生成数字的小三角`currentColor和color: inherit` ...

  5. Linux 0.11源码阅读笔记-块设备驱动程序

    块设备驱动程序 块设备驱动程序负责实现对块设备数据的读写功能.内核代码统一使用缓冲块间接和块设备(如磁盘)交换数据,缓冲区数据通过块设备驱动程序和块设备交换数据. 块设备的管理 块设备表 内核通过一张 ...

  6. 【Android开发】EasyPermissions 请求权限

    安卓6.0以后,开发应用的时候,仅在AndroidManifest.xml中申请权限已经不可以了,需要在代码中动态申请. 现在看一个google推出的机制:EasyPermissions 引入步骤: ...

  7. Docker入门(windows版),利用Docker创建一个Hello World的web项目

    Docker 当大家点开这篇博客的时候,相信大家对docker多多少少也有些认识了,最近学习docker这门技术,略微有些心得,写篇文章记录一下学习过程并帮大家跳过一些坑. docker的核心有两个, ...

  8. 「实践篇」解决微前端 single-spa 项目中 Vue 和 React 路由跳转问题

    前言 本文介绍的是在做微前端 single-spa 项目过程中,遇到的 Vue 子应用和 React 子应用互相跳转路由时遇到的问题. 项目情况:single-spa 项目,基座用的是 React,目 ...

  9. Python入门-常用模块

    1.sys,os import sys import os #获取当前的路径 print(sys.path[0]) print(os.getcwd()) print(os.path.abspath(& ...

  10. Spring Boot-场景启动器

    分析上文快速入门 1.查看pom文件导入的依赖(starter的父项目) <parent> <artifactId>spring-boot-starter-parent< ...