python初級(302) 2 easygui簡單使用

一、複習之前的兩個練習,鞏固計數循環和條件循環

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安裝成功

image_thumb1

 

三、運行第一個easygui程序

消息對話框:msgbox

1、新建個python文件guidemo1.py

image_thumb7

2、寫入代碼:

import easygui as g
g.msgbox("hello world!")

3、 運行guidemo1.py

image_thumb5

4 、運行結果

image_thumb9

 

三、easygui其他組件:

1、yes or no 對話框:ynbox(msg, title)

import easygui as g
rel = g.ynbox("你喜歡紅色嗎?", "title")
if rel:
    g.msgbox("你選擇是")
else:
    g.msgbox("你選擇否")

image

2、選擇對話框:choicebox(msg, title, choices)

import easygui as g
msg = "輸入你喜歡的顏色"
title = "遊戲互動"
choices = ["紅色", "綠色", "藍色", "青色"]
choice = g.choicebox(msg, title, choices)
g.msgbox("你喜歡的顏色是: " + choice)

image

3、按鈕對話框:buttonbox(msg, title, choices)

import easygui as g
msg = "輸入你喜歡的顏色"
title = "遊戲互動"
choices = ["紅色", "綠色", "藍色", "青色"]
choice = g.buttonbox(msg,  title, choices)
g.msgbox("你喜歡的顏色是: " + choice)

image

4、輸入對話框:enterbox(msg, title)

import easygui as g
text = g.enterbox("請輸入一句話", "title")
g.msgbox(text)

image

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章