一、複習之前的兩個練習,鞏固計數循環和條件循環
1、系統生成一個隨機數1到5,然後讓用戶的猜測,若猜對了,提示恭喜你,猜對了,否則提示,對不起,你猜錯了
(提示,1到5的隨機數爲:secret = random.randint(1, 5)),此行代碼之前需先引入隨機數模塊import random
2、將以下乘法口訣表代碼改爲while循環
for i in range(1, 9+1): text = "" for j in range(1, i+1): text += "{}*{}={:2} ".format(i,j,i*j) print(text)
二、easygui 安裝
1、命令行下敲入命令:pip install easygui
看到Successfully installed easygui字樣的,表示easygui安裝成功
三、運行第一個easygui程序
消息對話框:msgbox
1、新建個python文件guidemo1.py
2、寫入代碼:
import easygui as g g.msgbox("hello world!")
3、 運行guidemo1.py
4 、運行結果
三、easygui其他組件:
1、yes or no 對話框:ynbox(msg, title)
import easygui as g rel = g.ynbox("你喜歡紅色嗎?", "title") if rel: g.msgbox("你選擇是") else: g.msgbox("你選擇否")
2、選擇對話框:choicebox(msg, title, choices)
import easygui as g msg = "輸入你喜歡的顏色" title = "遊戲互動" choices = ["紅色", "綠色", "藍色", "青色"] choice = g.choicebox(msg, title, choices) g.msgbox("你喜歡的顏色是: " + choice)
3、按鈕對話框:buttonbox(msg, title, choices)
import easygui as g msg = "輸入你喜歡的顏色" title = "遊戲互動" choices = ["紅色", "綠色", "藍色", "青色"] choice = g.buttonbox(msg, title, choices) g.msgbox("你喜歡的顏色是: " + choice)
4、輸入對話框:enterbox(msg, title)
import easygui as g text = g.enterbox("請輸入一句話", "title") g.msgbox(text)