首页 > temp > python入门教程 >
-
Python模块 | EasyGui
学习记录,好记不如烂笔头
- 如有不对之处欢迎大佬指点 !
- 学习文章来源<点击访问>
- 记录平台 :
- Github
- Blog[大灰狼]
- HelloFlask 论坛( 官网 ) ( 镜像 )
- (代码写的很一般,轻喷..我目前还是初学者-未入门)
什么是 EasyGUI?
-
EasyGUI 是 Python 中一个非常简单的 GUI 编程模块,不同于其他的 GUI 生成器,它不是事件驱动的。相反,所有的
GUI 交互都是通过简地函数调用就可以实现 -
EasyGUI 为用户提供了简单的 GUI 交互接口,不需要程序员知道任何有关 tkinter,框架,部件,回调或 lambda
的任何细节。 - EasyGUI 可以很好地兼容 Python 2 和 3,并且不存在任何依赖关系。
- EasyGUI 是运行在 Tkinter 上并拥有自身的事件循环,而 IDLE 也是 Tkinter写的一个应用程序并也拥有自身的事件循环。因此当两者同时运行的时候,有可能会发生冲突,且带来不可预测的结果。因此如果你发现你的EasyGUI 程序有这样的问题,请尝试在 IDLE 外去运行你的程序。
[EasyGui中的函数]
通过如下方式可得知EasyGui中有19种函数方法:
- 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)





ynbox | 使用示例
ynbox() 函数的默认语法如下: (参数:title 看情况可用可不用) ynbox(msg='Shall I continue?', title=' ', choices=('Continue', 'Cancel'), image=None) 参数:choices 提供了两个按钮(返回值为布尔类型), 按钮: Continue返回True | 按钮: Cancel返回False


参数:choices 还可以设定快捷键
- 设置 F1~F12 为快捷键
- 设置 a~z 为快捷键


如果是以这种方式 [<>] 设置快捷键的话,按钮上的文字就显示该显示的, 如果是以这种方式 [] 设置快捷键的话, 按钮上会...很蠢, 也可能是我菜,没找到隐藏的方法
--------------------------------------------------------------------------------------------------------------------
ccbox | 使用示例
ccbox() 函数的默认语法等同于ynbox() 函数 参数:choices 的快捷键用法也是等同于ynbox() 函数 参数:choices 返回值也是为布尔类型, 不过写这篇笔记前看了网上相关的文章说ccbox() 函数是返回的为整数 1或0, 实测不对 ! 举个例子如下:


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


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() 函数返回按钮的文本内容
- 如果按钮中设置了快捷键, 那么快捷键这个字符串的内容也会被返回

![[<F1>]按钮](/articlelist/uploads/allimg/210609/21323I463-14.png)
![[<F2>]2](/articlelist/uploads/allimg/210609/21323L026-15.png)
![[<F3>]3](/articlelist/uploads/allimg/210609/21323M2L-16.png)

![[d])()*&Y^^%^%\\re123](/articlelist/uploads/allimg/210609/21323KC1-18.png)
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


choicebox | 使用示例
choicebox() 函数的默认语法如下: (参数:title 看情况可用可不用) choicebox(msg='Pick an item', title='', choices=[], preselect=0, callback=None, run=True) choicebox() 为用户提供了一个可选择的列表,使用序列(元祖或列表)作为选项,这些选项显示前会按照不区分大小写的方法排好序
- 举些例子:
- 1.在显示前不区分大小写的方法排好序表示怀疑, 因为代码运行后并没有看到选择项里的内容有排序
- 2.另外还可以使用键盘来选择其中一个选项(可多次点击选择 | 如果是以字母或数字开头的话), 顺便看下choicebox() 函数返回的类型




multchoicebox | 使用示例
multchoicebox() 函数的默认语法如下: (参数:title 看情况可用可不用) multchoicebox(msg='Pick an item', title='', choices=[], preselect=0, callback=None, run=True) multchoicebox() 函数也是提供一个可选择的列表, 与choicebox() 不同的是:
- multchoicebox() 支持用户选择 0 个, 1 个或者同时选择多个选项, 也可在选择多个选项后, 取消某些已选定的选项.
- multchoicebox() 函数的返回值是一个列表
- 双击列表中的选项返回空列表,必须通过“OK”按钮选择(这句话其实并不完全正确, 就是在测试操作中这种表达不绝对)



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]按钮返回文本区域显示的内容,
如果修改了文本区域的内容,则返回修改过的内容


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


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 之间的单个数字


passwordbox | 使用示例
passwordbox() 函数的默认语法如下: (参数:title 看情况可用可不用) passwordbox(msg='Enter your password.', title=' ', default='', image=None, root=None) passwordbox() 函数跟 enterbox() 函数用法一样, 不同的是用户输入的内容用星号 (*) 显示出来, 该函数返回用户输入的字符串


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 是把输入框内容返回成一个列表的, 因此可以利用此特性做一些判断, 比如说
必选项(不能为空)
输入框中内容限制规则
等- 如果用户取消操作, 则返回域中的列表的值或者 None 值


⇔


multpasswordbox | 使用示例
multpasswordbox() 函数的默认语法如下: (参数:title 看情况可用可不用) multpasswordbox(msg='Fill in values for the fields.', title=' ', fields=(), values=(), callback=None, run=True) 使用方法上可以说是跟
multenterbox() 函数
一样, 唯一的区别就是multpasswordbox() 函数
提供的最后一个
输入框显示为密码的形式(*)


filesavebox | 使用示例
filesavebox() 函数的默认语法如下: (参数:title 看情况可用可不用) filesavebox(msg=None, title=None, default='', filetypes=None)
- filesavebox() 函数提供一个对话框,
让
用户选择文件需要保存的路径
(带完整路径),如果用户选择 “Cancel” 按钮则返回 None- 参数default 应该包含一个文件名(例如当前需要保存的文件名),当然也可以设置为空, 或者包含一个文件格式掩码的通配符
- 关于 filetypes 参数的设置方法:
- 可以是包含文件掩码(
或者是叫后缀名??
)的字符串列表,例如:filetypes = ["*.txt"]- 可以是字符串列表,列表的最后一项字符串是文件类型的描述,例如:filetypes = [".css", [".htm", "*.html", "HTML files"]]
- 举个例子: 打开一个文件, 然后变动下内容, 判断内容是否变动, 如果有变动则提示保存
1.打开文件后如果直接关闭窗口或者点击了"Cancel"按钮
,(原文件内容不会有变化也不会有新的文件被保存到硬盘上)看下原文件内容,如图:
2.如果文件内容发生变化,在选择窗口步骤中不小心关闭了窗口或者点击了"取消操作"按钮
,(原文件内容不会有变化也不会有新的文件被保存到硬盘上)看下原文件内容,如图:
3.如果文件内容发生变化,在选择窗口步骤点击了"另存为"按钮
,(原文件内容不会有变化, 新的文件会被保存在选择的完整路径下)如下图:
4.如果文件内容发生变化,在选择窗口步骤点击了"覆盖"按钮
,(原文件内容会产生变化,并被覆写为新内容且保存)如下图:
fileopenbox | 使用示例
fileopenbox() 函数的默认语法如下: (参数:title 看情况可用可不用)
用法上和filesavebox() 函数差不多
fileopenbox(msg=None, title=None, default='*', filetypes=None, multiple=False)
- 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 则表示可以同时选择多个文件,举个例子看图:

diropenbox | 使用示例
diropenbox() 函数的默认语法如下: (参数:title 看情况可用可不用) diropenbox(msg=None, title=None, default=None) diropenbox() 函数用于提供一个对话框,返回用户选择的目录名(带完整路径), 如果用户选择 “Cancel” 则返回 None


__EOF__