与孩子一起学编程06章

这一章,开始接触到图形界面,GUI用的是easygui,google后得到。

>>> import easygui
>>> 

要使用模块前,这是必须的,import模块。

>>> import easygui
>>> easygui.msgbox("Hello world!!")
'OK'
>>> 

还可以这样

>>> user_response = easygui.msgbox("Hello world!!")
>>> print user_response
OK
>>> 

下面来一个用图形界面选择按钮,代码如下

# 与孩子一起学编程06章
# 2013年8月25日22:25:19
# easygui 使用图形界面 6-1
import easygui
flavor = easygui.buttonbox("What is your favorite ice cream flavor?",
                           choices = ['Vanilla', 'Chocolate', 'Strawberry'])

easygui.msgbox("You picked " + flavor)
还可以这样

# 与孩子一起学编程06章
# 2013年8月25日22:45:27
# easygui 使用图形界面 6-2
import easygui
flavor = easygui.choicebox("What is your favorite ice cream flavor?",
                           choices = ['Vanilla', 'Chocolate', 'Strawberry'])

easygui.msgbox("You picked " + flavor)

还可以用输入框自己输入

# 与孩子一起学编程06章
# 2013年8月25日22:46:37
# easygui 使用输入框得到输入 6-3
import easygui
flavor = easygui.enterbox("What is your favorite ice cream flavor")

easygui.msgbox("You entered " + flavor)

默认输入,这样的

# 与孩子一起学编程06章
# 2013年8月25日22:51:36
# easygui 使用默认输入 6-4
import easygui
flavor = easygui.enterbox("What is your favorite ice cream flavor",
                          default = 'Vanilla')

easygui.msgbox("You entered " + flavor)
这样,就有个默认输入。


怎么输入数字,下面是一个前面用过的一个猜数字游戏,这次,换用图形界面来猜,试试看

# 与孩子一起学编程06章
# 2013年8月25日22:57:48
# 使用easygui的猜数游戏 6-5
import random, easygui

secret = random.randint(1, 99)
guess = 0
tries = 0

easygui.msgbox("""AHOY! I'm the Dread Pirate Roberts, and I have a secret!
It is a number from 1 to 99. I'll give you 6 tries.""")

while guess != secret and tries < 6:
    guess = easygui.integerbox("What's yer guess, matey")
    if not guess: break
    if guess < secret:
        easygui.msgbox(str(guess) + " is too low, ye scurvy dog!")
    elif guess > secret:
        easygui.msgbox(str(guess) + " is too high, landlubber!")
    tries = tries + 1

if guess == secret:
    easygui.msgbox("Avast! Ye got it! Found my secret, ye did!")
else:
    easygui.msgbox("No more guesses! Better luck next time, matey!")

嗯,还是挺有意思的,是吧,好玩的python,越来越有意思了,呵呵。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值