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

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