1.相关知识:
EasyGui官网:
http://easygui.sourceforge.net
官方教学文档:
-easygui-docs-0.96\tutorial\index.html
2.导入EasyGui:
方法1:
import easygui
easygui.msgbox('hello world')
方法2:
from easygui import *
msgbox('hello world')
方法3(推荐):
import easygui as g
g.msgbox('hello world')
3.使用easygui:
说明:建议不要在IDLE上运行EasyGui,因为本身IDLE也是写在Tkinter在的一个应用程序并且拥有自身的事件循环,两者同时运行的时候可能会发生冲突。
使用实例:
import easygui as g
import sys
while 1:
g.msgbox('嗨,欢迎进入第一个界面小游戏')
msg='请问你希望在鱼C工作室学习到什么知识呢?'
title='小游戏互动'
choices=['谈恋爱','编程','跑步','琴棋书画']
choice=g.choicebox(msg,title,choices)
g.msgbox('你的选择是:' + str(choice),'结果')
msg ='你希望重新开始小游戏吗?'
title='请选择'
if g.ccbox(msg,title):
pass
else:
sys.exit(0)
运行结果:
以下部分转载自:
无知小德
4.easygui内置方法:
(1)msgbox():
用法说明:
msgbox(msg=’(Your message goes here)’, title=’ ', ok_button=‘OK’, image=None, root=None)
示例代码:
import easygui as g
#比如msgbox()函数标题部分的参数就是可选的,所以你调用msgbox的时候可以指定一个消息参数,例如:
msg = g.msgbox('hello easy gui!')
#当然你也可以指定信息参数和标题参数
title = g.msgbox(msg = '我一定要学好编程!', title = '奥利给', ok_button = '加油')
实现结果:
(2)ccbox()
用法说明:ccbox(msg=‘Shall I continue?’, title=’ ', choices=(‘Continue’, ‘Cancel’), image=None)
ccbox() 提供一个选择:Continue 或者 Cancel,并相应的返回 1(选中Continue)或者 0(选中Cancel)。注意 ccbox() 是返回整型的 1 或 0,不是布尔类型的 True 或 False。但你仍然可以这么写:
import sys
import easygui as g
#注意choices中第一个参数对应逻辑值1(执行if条件中代码),第二个参数对应执行逻辑值0(else中代码)
if g.ccbox('确定退出游戏吗?', choices = ('再考虑一下', '退出')):
g.msgbox('你还是太年轻了!')
else:
sys.exit(0)
运行结果:
(3)ynbox()
(4)buttonbox():
用法说明:buttonbox(msg=’’, title=’ ', choices=(‘Button1’, ‘Button2’, ‘Button3’), image=None, root=None)
可以使用 buttonbox() 定义自己的一组按钮,buttonbox() 会显示一组你定义好的按钮。当用户点击任意一个按钮的时候,buttonbox() 返回按钮的文本内容。如果用户取消或者关闭窗口,那么会返回默认选项(第一个选项)。
代码实例:
import easygui as g
temp = g.buttonbox(msg = '你的兴趣爱好是?', title = '调查', choices = ('跑步', '乒乓球', '篮球'))
print(temp)#此时会打印所选择的选项对应的文本内容
(5)indexbox()
用法说明:indexbox(msg=‘Shall I continue?’, title=’ ', choices=(‘Yes’, ‘No’), image=None)
用法例子:
import easygui as g
temp = g.indexbox(msg = '您确定要退出吗?', title = '调查', choices = ('确定', '考虑'))
print(temp)#此时当点击第一个按钮返回值为0,第二个按钮返回值为1
(6)boolbox()
用法说明:
boolbox(msg=‘Shall I continue?’, title=’ ', choices=(‘Yes’, ‘No’), image=None)
用法例子:
import easygui as g
temp = g.indexbox(msg = '您确定要退出吗?', title = '调查', choices = ('确定', '考虑'))
print(temp)#此时当点击第一个按钮返回值为1,第二个按钮返回值为0
(7)buttonbox()中显示图片:
用法说明:buttonbox函数(例如msgbox(),ynbox(),indexbox()等等),对关键字image赋值,python仅支持.gif格式图像
实例分析:
import easygui as g
g.buttonbox(msg = '图片如下:', image = r'C:\Users\X\Desktop\8-实例7图1.gif', choices = ('1', '2', '3'))
(8)choicebox()–
用法说明:choicebox(msg=‘Pick something.’, title=’ ', choices=())
choicebox为用户提供了一个可以选择的列表,使用序列(元组或者列表)作为选项,这些选项显示前会按照不区分大小写方法排序
另外还可以使用键盘来选择其中一个选项:
-例如当按下键盘上的"g"键,将会选中的第一个以"g"开头的选项。
-再次按下"g"键,则会选中下一个以"g"开头的选项。
-在选中最后一个以"g"开头的选项的时候,再次按下"g"键将重新回到在列表的开头的第一个以"g"开头的选项。
-如果选项中没有以"g"开头的,则会选中字符排序在"g"之前(“f”)的那个字符开头的选项。
-如果选项中没有字符的排序在"g"之前的,那么在列表中第一个元素将会被选中。
实例分析:
import easygui as g
msg = '选择一个你喜欢的业余生活'
title = ''
choices_list = ['看书', '游泳', '骑自行车', '玩游戏']
reply = g.choicebox(msg, title, choices = choices_list)
print(reply)#会显示你所选结果
结果:
(9)mutchoicebox()
用法示例:multchoicebox(msg=‘Pick as many items as you like.’, title=’ ', choices=(), **kwargs)
–multchoicebox() 函数也是提供一个可选择的列表,与 choicebox() 不同的是,multchoicebox() 支持用户选择 0 个,1 个或者同时选择多个选项。
–multchoicebox() 函数也是使用序列(元祖或列表)作为选项,这些选项显示前会按照不区分大小写的方法排好序。
实例:
import easygui as g
msg = '选择一个你喜欢的业余生活'
title = ''
choices_list = ['看书', '游泳', '骑自行车', '玩游戏']
reply = g.multchoicebox(msg, title, choices = choices_list)
print(reply)#返回所选,若多于一个,则返回值为列表
结果图示:
10.enterbox()–输入框
用法说明:enterbox(msg=‘Enter something.’, title=’ ‘, default=’’, strip=True, image=None, root=None)
–enterbox() 为用户提供一个最简单的输入框,返回值为用户输入的字符串。
–默认返回的值会自动去除首尾的空格,如果需要保留首尾空格的话请设置参数 strip=False。
实例说明:
import easygui as g
msg = '你喜欢的业余生活'
title = '业余'
reply = g.enterbox(msg, title)
print(reply)#返回你输入的字符串的值
图示:
11.integerbox()
用法说明:integerbox(msg=’’, title=’ ‘, default=’’, lowerbound=0, upperbound=99, image=None, root=None, **invalidKeywordArguments)
–integerbox() 为用户提供一个简单的输入框,用户只能输入范围内(lowerbound参数设置最小值,upperbound参数设置最大值)的整型数值,否则会要求用户重新输入。
实例:
import easygui as g
msg = '输入得分:'
title = '计算分数'
reply = g.integerbox(msg, title, lowerbound = 0, upperbound = 100)
print(reply)
图示:
12.multenterbox()
用法说明:multenterbox(msg=‘Fill in values for the fields.’, title=’ ', fields=(), values=())
multenterbox() 为用户提供多个简单的输入框,要注意以下几点:
- 如果用户输入的值比选项少的话,则返回列表中的值用空字符串填充用户为输入的选项。
- 如果用户输入的值比选项多的话,则返回的列表中的值将截断为选项的数量。
- 如果用户取消操作,则返回域中的列表的值或者None值
实例:
import easygui as g
msg = '请填写信息(其中带*号为必填项)'
title = '账号中心'
fieldNames = ['*用户名', '*真实姓名', '固定电话', '*手机号码', 'QQ', 'Email']
fieldValues = []
fieldValues = g.multenterbox(msg, title, fieldNames)
while True:
if fieldValues == None:
break
errmsg = ''
for i in range(len(fieldNames)):
option = fieldNames[i].strip()
if fieldValues[i].strip() == '' and option[0] == '*':
errmsg += ('【%s】为必填项 ' % fieldNames[i])
if errmsg == '':
break
fieldValues = g.multenterbox(msg, title, fieldNames, fieldValues)
print('您填写的资料如下:%s' % str(fieldValues))#打印输入的信息
运行结果:
13.passwordbox()
用法说明:passwordbox(msg=‘Enter your password.’, title=’ ‘, default=’’, image=None, root=None)
passwordbox() 跟 enterbox() 样式一样,不同的是用户输入的内容用"*"显示出来,返回用户输入的字符串:
用法示例:
import easygui as g
password_input = g.passwordbox(msg = '请输入您的密码')
print(password_input)#打印返回的字符串
运行结果:
14.multpasswordbox()
用法说明:multpasswordbox(msg=‘Fill in values for the fields.’, title=’ ', fields=(), values=())
multpasswordbox() 跟 multenterbox() 使用相同的接口,但当它显示的时候,最后一个输入框显示为密码的形式("*")
实例:
import easygui as g
msg = '请输入用户名和密码'
title = '用户登录接口'
user_info = []
user_info = g.multpasswordbox(msg, title, ('用户名', '密码'))
print(user_info)#打印输入的信息
运行结果:
15.textbox()
用法说明:
textbox(msg=’’, title=’ ‘, text=’’, codebox=0)
extbox() 函数默认会以比例字体(参数 codebox=1 设置为等宽字体)来显示文本内容(会自动换行哦),这个函数适合用于显示一般的书面文字。
注:text 参数(第三个参数)可以是字符串类型,列表类型,或者元祖类型。
实例:
import easygui as g
msg = '文本内容如下:'
title = '内容显示'
text = 'hdashdahdhojoosa'
g.textbox(msg, title, text)
运行结果:
16.diropenbox():
用法说明:
diropenbox(msg = None, title = None, default = None)
dieopenbox()函数提供一个对话框,返回用户选择的目录名(带完整路径),如果用户选择cancel,则返回值为None
default参数用来设置默认打开目录(请确保目录已经存在)
import easygui as g
import sys
file_path = g.diropenbox('请选择一个文件夹', '资源管理器', 'C:/Users/X/Desktop')
g.msgbox(file_path, '文件夹路径')#显示所选文件夹路径
结果:
17.fileopenbox():
用法说明:fileopenbox(msg = None, title = None, default = ‘*’, filetypes = None),fileopenbox()函数用于提供一个对话框,返回用户选择的文件名(带完整路径),当用户选择cancel时会返回None
关于default参数的设置方法:
- default参数制定了一个默认路径,通常包含一个或者多个通配符
- 如果设置了default参数,fileopenbox()显示默认的文件路径格式
- default默认参数是’’,即匹配所有格式的文件
例如:
1.default = 'c:/fishc/.py’即显示c:\fishc文件夹下所有的python文件
2.default = 'c:/fishc/test*.py’即显示c:\fishc文件夹下所有的名字以test开头的python文件
关于fieltypes参数的设置方法:
- 可以包含文件掩码的字符串列表,例如:filetypes = [’*.txt’]
- 可以实字符串列表,列表最后一项字符串是文件类型的描述,例如:filetypes = [’.css’, [’.htm’,’*.html’,‘HTML files’]]
用法实例:
import easygui as g
import sys
file_path = g.fileopenbox('请选择一个文件名', '资源管理器', 'C:/Users/X/Desktop', '*.txt')
g.msgbox(file_path, '文件夹路径')#显示所选文件夹路径
f_txt = open(file_path)#打开文件
content = f_txt.read()
f_txt.close()#关闭文件
g.textbox(msg = '文件[' + file_path +']内容如下:', title = '文本内容显示',text = content)
18.filesavebox()
用法说明:filesavebox(msg = None, title = None, default = ‘’, filetypes = None)
filesavebox()函数提供一个对话框,用于选择文件需要保存的路径,用户选择cancel时返回值为None
default参数应该包含一个用户名(例如当前需要保存的文件名),当然也可以设置为空,或者设置为一个包含文件格式掩码的通配符
filetypes参数设置:
- 可以包含文件掩码的字符串列表,例如:filetypes = [’*.txt’]
- 可以实字符串列表,列表最后一项字符串是文件类型的描述,例如:filetypes = [’.css’, [’.htm’,’*.html’,‘HTML files’]]
用法示例:
import easygui as g
import sys
msg = '请保存文件'
title = '资源管理器'
default = 'C:/Users/X/Desktop/*'
path = g.filesavebox(msg, title, default, filetypes = ['*.txt'])
print(path)
19.exceptionbox()
用法说明:使用easygui编写gui程序,有时候会发生异常。当然这取决于你如何运行你的应用程序,当你的应用程序崩溃的时候,堆栈追踪可能会被抛出,或者被写入到stout标准输出函数中
easygui通过exceptionbox()函数提供了更好的方式去处理异常,异常出现的时候,exceptionbox()会显示堆栈追踪在一个codebox()中并且允许你做进一步的处理。
实例分析:
import easygui as g
import sys
import os
try:
print('hello world!')
int('dash')#这里产生异常
except:
g.exceptionbox()
小结:(用法总结表格)
名称 | 用法 | 附加说明 |
---|---|---|
msgbox() | msgbox(msg=’(Your message goes here)’, title=’ ', ok_button=‘OK’, image=None, root=None) | msgbox()显示一个消息和一个OK按钮,按钮内容可以修改 |
ccbox() | ccbox(msg=‘Shall I continue?’, title=’ ', choices=(‘Continue’, ‘Cancel’), image=None) | ccbox() 提供一个选择:Continue 或者 Cancel,并相应的返回 1(选中Continue)或者 0(选中Cancel)返回值为整型(int) |
buttonbox() | buttonbox(msg=’’, title=’ ', choices=(‘Button1’, ‘Button2’, ‘Button3’), image=None, root=None) | 用户点击任意一个按钮的时候,buttonbox() 返回按钮的文本内容。如果用户取消取消或者关闭窗口,那么会返回默认选项(第一个选项) |
indexbox() | indexbox(msg=‘Shall I continue?’, title=’ ', choices=(‘Yes’, ‘No’), image=None) | 当用户选择第一个按钮的时候返回序列号0,选择第二个按钮时候返回序列号1。 |
boolbox() | boolbox(msg=‘Shall I continue?’, title=’ ', choices=(‘Yes’, ‘No’), image=None) | 当点击第一个按钮返回值为1,第二个按钮返回值为0 |
button()显示图片 | buttonbox(msg=’’, title=’ ', choices=(‘Button1’, ‘Button2’, ‘Button3’), image=图片路径, root=None | 图片必须为gif类型 |
choicebox() | choicebox(msg=‘Pick something.’, title=’ ', choices=()) | choicebox() 为用户提供了一个可选择的列表,使用序列(元祖或列表)作为选项,这些选项显示前会按照不区分大小写的方法排好序。 |
mutchoicebox() | multchoicebox(msg=‘Pick as many items as you like.’, title=’ ', choices=(), **kwargs) | multchoicebox() 函数也是提供一个可选择的列表,与 choicebox() 不同的是,multchoicebox() 支持用户选择 0 个,1 个或者同时选择多个选项。multchoicebox() 函数也是使用序列(元祖或列表)作为选项,这些选项显示前会按照不区分大小写的方法排好序。 |
enterbox() | enterbox(msg=‘Enter something.’, title=’ ‘, default=’’, strip=True, image=None, root=None) | enterbox() 为用户提供一个最简单的输入框,返回值为用户输入的字符串。默认返回的值会自动去除首尾的空格,如果需要保留首尾空格的话请设置参数 strip=False。 |
integerbox() | integerbox(msg=’’, title=’ ‘, default=’’, lowerbound=0, upperbound=99, image=None, root=None, **invalidKeywordArguments) | integerbox() 为用户提供一个简单的输入框,用户只能输入范围内(lowerbound参数设置最小值,upperbound参数设置最大值)的整型数值,否则会要求用户重新输入。 |
multenterbox() | multenterbox(msg=‘Fill in values for the fields.’, title=’ ', fields=(), values=()) | multenterbox() 为用户提供多个简单的输入框,要注意以下几点:[1]如果用户输入的值比选项少的话,则返回列表中的值用空字符串填充用户为输入的选项。[2]如果用户输入的值比选项多的话,则返回的列表中的值将截断为选项的数量。[3]如果用户取消操作,则返回域中的列表的值或者None值。 |
passwordbox() | passwordbox(msg=‘Enter your password.’, title=’ ‘, default=’’, image=None, root=None) | passwordbox() 跟 enterbox() 样式一样,不同的是用户输入的内容用"*"显示出来,返回用户输入的字符串 |
multpasswordbox() | multpasswordbox(msg=‘Fill in values for the fields.’, title=’ ', fields=(), values=()) | multpasswordbox() 跟 multenterbox() 使用相同的接口,但当它显示的时候,最后一个输入框显示为密码的形式("*") |
textbox() | textbox(msg=’’, title=’ ‘, text=’’, codebox=0) | textbox() 函数默认会以比例字体(参数 codebox=1 设置为等宽字体)来显示文本内容(会自动换行哦),这个函数适合用于显示一般的书面文字。注:text 参数(第三个参数)可以是字符串类型,列表类型,或者元祖类型。 |
diropenbox() | diropenbox(msg = None, title = None, default = None) | dieopenbox()函数提供一个对话框,返回用户选择的目录名(带完整路径),如果用户选择cancel,则返回值为None–>default参数用来设置默认打开目录(请确保目录已经存在) |
fileopenbox() | fileopenbox(msg = None, title = None, default = ‘*’, filetypes = None) | 见上方第17部分介绍 |
filesavebox() | filesavebox(msg = None, title = None, default = ‘’, filetypes = None) | 见上方第18部分介绍 |
exceptionbox() | 见上方第19部分介绍 |