函數介紹
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()
更加實用的類型,與上面類似,不過其隻有最後一個框是匿名的,即密碼輸入;
其他都一樣,傳回值為清單形式;
猜數字遊戲
代碼如下:
import random
import easygui as eg
eg.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("遊戲結束,不玩啦^_^")