1.先上源代码
import random
import easygui as eg
def guess_ordinary():
answer = random.randint(0, 11)
user_answer = int(eg.enterbox(msg = "请在0-10中选择一个整数: ", title = "猜数字"))
if user_answer == answer:
eg.msgbox(msg = "恭喜你,猜对了!", title = "猜数字", ok_button = "继续")
else:
eg.msgbox(msg = "很遗憾,你猜错了!", title = "猜数字", ok_button = "继续")
dywc = eg.choicebox(msg = "是否重新游戏? ", title = "猜数字", choices = ["是", "否"])
if dywc == "是":
guess_ordinary()
else:
return 1
def guess_secondary():
answer = random.randint(0, 101)
def GS_main(answer):
user_answer = int(eg.enterbox(msg = "请在0-100中选择一个整数: ", title = "猜数字"))
if user_answer == answer:
eg.msgbox(msg = "恭喜你,猜对了!", title = "猜数字", ok_button = "继续")
else:
if user_answer > answer:
eg.msgbox(msg = "你猜的数字太大了!", title = "猜数字", ok_button = "继续")
else:
eg.msgbox(msg = "你猜的数字太小了!", title = "猜数字", ok_button = "继续")
GS_main(answer)
GS_main(answer)
dywc = eg.choicebox(msg = "是否重新游戏? ", title = "猜数字", choices = ["是", "否"])
if dywc == "是":
guess_ordinary()
else:
return 1
def main():
user_answer = eg.ccbox(msg = "欢迎来到猜数字!\n\n请选择游戏难度: ", title = "猜数字", choices = ["经典模式0-10", "经典模式0-100"])
if user_answer == "经典模式0-10":
result1 = guess_ordinary()
elif user_answer == "经典模式0-100":
result2 = guess_secondary()
main()
2.easygui库讲解
2.1.导库
先确保你有python,
python
在编辑器中输入
import easygui
# 或
from easygui import*
这里我使用 eg 作为easygui库的别名。
2.2.功能讲解
2.2.1.msgbox()
纯显示内容。
import easygui
easygui.msgbox(msg = "hello world")
msg参数为显示内容,title参数为标题,ok_button参数为确认按钮的内容。(后面所有的函数都有这些功能)
2.2.2.enterbox()
有一个文本框,需用变量保存结果。
msg参数为显示内容,title参数为标题,ok_button参数为确认按钮的内容。
2.2.3.choicebox()
有一个选择框,需用变量保存结果。
msg参数为显示内容,title参数为标题,ok_button参数为确认按钮的内容,choices参数为选择框的选项。标签:title,python,eg,msg,可视化,answer,easygui,button From: https://blog.csdn.net/a_pjx_z/article/details/143982380