python初級(302) 3 easygui簡單使用二

一、複習

1、easygui 信息提示對話框

2、easygui 是否對話框

 

二、easygui其它組件

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

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

image_thumb3

 

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

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

image_thumb5[1]

 

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

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

image_thumb8

 

4、多項輸入對話框:multenterobx(msg, title, fields=[])

import easygui as g
name, pass_ward = g.multenterbox("登錄", "title", ["賬號:", "密碼:"])
print(name)
print(pass_ward)

image

 

三、作業

1、將課堂練習照着在計算機上運行一遍

2、以下爲猜數遊戲的源代碼,請將輸入用輸入對話框,print函數用信息提示對話框改寫成一個gui的程序

import random
secret = random.randint(1, 100)
print("請猜一個1到100的數,你有6次機會")
success = 0
for i in range(6):
    guess = int(input("請猜數:"))
    if guess < secret:
        print("你猜的數太小了")
    elif guess > secret:
        print("你猜的數太大了")
    else:
        success = 1
        break
if success == 1:
    print("恭喜你,你猜對了")
else:
    print("對不起,你猜錯了,祕密數爲:", secret)

 

四、參考答案:

import random
import easygui as g

secret = random.randint(1, 100)
g.msgbox("請猜一個1到100的數,你有6次機會")
success = 0
for i in range(6):
    guess = int(g.enterbox("請輸入你要猜的數"))
    if guess < secret:
        g.msgbox("你猜的數太小了")
    elif guess > secret:
        g.msgbox("你猜的數太大了")
    else:
        success = 1
        break
if success == 1:
    g.msgbox("恭喜你,你猜對了")
else:
    g.msgbox("對不起,你猜錯了,祕密數爲:" + str(secret))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章