导入easygui模块有很多种方法 , 这里只介绍一种简单使用的 .
1 import easygui as g
将easygui 简称为g 然后开始调用她的函数就行.
1 import easygui as g 2 import sys 3 while 1: 4 g.msgbox("显示一个窗口并且显示这些文字")# 只显示一个对话框并且只有一个ok 5 msg="你希望学到什么呢?" 6 title="小游戏互动" # 在左上角的 标题旷里面 7 choices=['谈恋爱','编程','ooxx','琴棋书画'] # 在选择框内 , 提供可选择项 8 choice=g.choicebox(msg,title,choices) # 在这里 choice 可以得到上面你选择的那个选项 9 g.msgbox("你的选择是:"+str(choice),'结果') # 打印出来10 msg='你希望再来一次么?' 11 title='请选择'12 if g.ccbox(msg,title): # ok为真 cancel为假13 pass14 else:15 exit(0) # 用于退出程序 .
1 >>> import easygui as g2 >>> g.msgbox('我爱博主','人民心声')3 'OK'
在函数中有许多的默认参数 如下
1 import easygui as g2 import sys3 choices=['愿意','不愿意','听从您的吩咐']4 reply=g.choicebox('你愿意和我在一起么,美女.',choices=choices)5 g.msgbox(reply)
msgbox的函数定义如下
1 >>> help(g.msgbox)2 Help on function msgbox in module easygui:3 4 msgbox(msg='(Your message goes here)', title=' ', ok_button='OK', image=None, root=None)5 Display a messagebox
这里示范一下修改按钮的办法 .
1 g.msgbox('are you ready?',ok_button='呦我草')
关于ccbox
1 msgbox(msg='(Your message goes here)', title=' ', ok_button='OK', image=None, root=None)
1 if g.ccbox('要再来一次吗?', choices=('要啊要啊^_^', '算了吧T_T')):2 g.msgbox('不给玩了,再玩就玩坏了......')3 else:4 sys.exit(0) # 记得先 import sys 哈
关于buttonbox
1 buttonbox(msg='', title=' ', choices=('Button1', 'Button2', 'Button3'), image=None, root=None)
1 g.msgbox(g.buttonbox("我爱你",'你说爱不爱',('唉','你唉','爱爱爱','爱爱爱')))
如何添加图片.
1 import easygui as g2 import sys3 g.buttonbox('大家说我长得帅吗?', image='D:/Documents/Pictures/2.gif', choices=('帅', '不帅', '!@#$%'))
为用户提供一组选项.
1 multchoicebox(msg='Pick as many items as you like.', title=' ', choices=(), **kwargs)2 Present the user with a list of choices.3 allow him to select multiple items and return them in a list.4 if the user doesn't choose anything from the list, return the empty list.5 return None if he cancelled selection.
1 import easygui as g2 str1=g.multchoicebox('你现在最想干啥','这是标题',('抽烟','打游戏','和蔡伟伟一起玩','学东西写代码','唉,日了狗了'))3 if str1==None:4 g.msgbox('如果点击取消的话 会返回一个 None')5 print(str1)6 g.msgbox(str1)
让用户输入
1 enterbox(msg='Enter something.', title=' ', default='', strip=True, image=None, root=None) 2 Show a box in which a user can enter some text. 3 4 You may optionally specify some default text, which will appear in the 5 enterbox when it is displayed. 6 7 Returns the text that the user entered, or None if he cancels the operation. 8 9 By default, enterbox strips its result (i.e. removes leading and trailing10 whitespace). (If you want it not to strip, use keyword argument: strip=False.)11 This makes it easier to test the results of the call::12 13 reply = enterbox(....)14 if reply:15 ...16 else:
1 >>> str1=g.enterbox(msg='请输入你想说的话', title='交互界面 ', default='这是默认用户输入', strip=True, image='D:/Documents/Pictures/2.gif', root=None)
让用户输入数据
integerbox(msg='', title=' ', default='', lowerbound=0, upperbound=99, image=None, root=None, **invalidKeywordArguments)
1 >>> g.integerbox(msg='输入你的分数 0~150', title='分数采集 ', default=0, lowerbound=0, upperbound=150)2 150
显示文本文档.
1 import easygui as g2 f=open('C:/Users/Administrator/Desktop/新建文本文档.txt')3 str1=f.read()4 g.textbox('下面是题解','求模',str1,1)
输入帐号密码.
1 passwordbox(msg='Enter your password.', title=' ', default='', image=None, root=None)
g.msgbox(g.passwordbox('输入你的密码','登陆提示','123456789'))
帐号密码登录框
1 multpasswordbox(msg='Fill in values for the fields.', title=' ', fields=(), values=())2 Same interface as multenterbox. But in multpassword box,3 the last of the fields is assumed to be a password, and4 is masked with asterisks.
import easygui as g(account,password)=g.multpasswordbox('请输入您的账号密码','登录框',('帐号','密码'))g.msgbox('帐号: '+account+'\n'+'密码: '+password)
目录和文件
1 import easygui as g2 #gui编程中常见的一个场景是要求用户输入目录或者文件名3 #easygui提供了一些基本的函数让用户来浏览文件系统,选择一个目录或者文件.4 str1=g.diropenbox('选择文件目录','浏览文件夹','C:/Users/Administrator/Desktop')5 g.msgbox(str1)
选择一个文件并且返回包括文件名的目录
1 fileopenbox(msg=None, title=None, default='*', filetypes=None)
1 import easygui as g2 #fileopenbox()函数用于提供一个对话框 , 返回用户选择的文件名(带完整路径)3 #如果用户选择 Cancel则返回None.4 str1=g.fileopenbox('选择文件','提示','C:/Users/Administrator/Desktop/__pycache__')5 g.msgbox(str1)