Python 图形化编程入门--交互模式,,函数介绍msgbox


函数介绍

msgbox()——消息弹窗

msgbox(msg=‘ ‘, title=‘ ‘, ok_button=‘ ‘, image=None, root=None)
该函数一般调用前三个关键字即可,加载图片的话给image赋值需要下载其他库,否则只能加载GIF。

ccbox()——双项选择

ccbox(msg=‘ ‘, title=‘ ‘, choices=(‘ ‘, ‘ ‘), image=None)
多了一个选项为choices(只能容纳两个选项!)
其返回值为布尔值Ture或者False.

buttombox()——多项选择

buttonbox(msg=‘ ‘, title=‘ ‘, choices=(‘Button1‘, ‘Button2‘, ‘Button3‘), image=None, root=None)
该函数和ccbox()不一样,其返回值为button的对应文本字符。

choicebox()、multchoicebox()——可选的下拉列表

choicebox(msg=‘ ‘, title=‘ ‘, choices=())
选项输入不再是单个元素,此处是以整个序列的方式输入,如列表、元组等;
选择选项后确认,会返回选项内容的文本内容,否则是none
multchoicebox()功能同样,只是他可以提供多选,多选的返回值是多选的文本列表

enterbox()——文本输入框

enterbox(msg=‘ ‘, title=‘ ‘, default=‘ ‘, strip=True, image=None, root=None)
其中,default关键字定义的是文本框默认值,strip的值为True时会自动忽略输入的首尾空格,False则相反;
返回值为输入的字符串;

interbox()——文本输入框

integerbox(msg=‘‘, title=‘ ‘, default=‘‘, lowerbound=0, upperbound=99, image=None, root=None, **invalidKeywordArguments)
该文本框只能输入界定范围内的整型数,返回值为输入值。

mulenterbox()——文本输入框

multenterbox(msg=‘ ‘, title=‘ ‘, fields=(), values=())
其中values是输入的默认值、feilds是需要填写的条目名称,均用列表填写;
返回值是所有填写的值构成的列表;

passwordbox()——密码输入框(不显示)

passwordbox(msg=‘ ‘, title=‘ ‘, default=‘ ‘, image=None, root=None)
基本关键字上面都介绍过了:提示语、标题、默认值等;
与文本框也类似,只是显示时会是*,更接近密码输入;
返回值依然是输入文本;

multpasswordbox()

更加实用的类型,与上面类似,不过其只有最后一个框是匿名的,即密码输入;
其他都一样,返回值为列表形式;

buttombox()——多项选择

buttonbox(msg=‘ ‘, title=‘ ‘, choices=(‘Button1‘, ‘Button2‘, ‘Button3‘), image=None, root=None)
该函数和ccbox()不一样,其返回值为button的对应文本字符。

猜数字游戏

代码如下:

import randomimport easygui as egeg.msgbox("欢迎进入猜数字小游戏!",image=‘main.gif‘)secret = random.randint(1,10)print(secret)msg = "猜一下我现在心里想的是哪个数字(1~10):"title = "数字小游戏"guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10,image=‘guess.gif‘)while True:    if guess == secret:        eg.msgbox("我草,你也太厉害了!",image=‘right.gif‘)        break    else:        if guess > secret:            eg.msgbox("哎,大了大了~~~",image=‘error.gif‘)        else:            eg.msgbox("嘿,小了小了~~~",image=‘error2.gif‘)        guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10,image=‘guess.gif‘)eg.msgbox("游戏结束,不玩啦^_^")

运行效果:

技术图片

技术图片
技术图片
技术图片
技术图片

温馨提示:

1.可以添加图片,但图片的路径要写对,否则无法识别
2.也可以添加其他功能,比如猜错之后直接点击退出按钮等

Python 图形化编程入门--交互模式

评论关闭