EasyGui设计一个简单的计算器

计算器功能:两个整数的加减乘除。刚刚开始学所以功能很简单。
#设计一个计算器,用于两个数的加减乘除,并显示结果
#要用到easygui实现界面交互

#设计一个计算器,用于两个数的加减乘除,并显示结果
#要用到easygui实现界面交互


import easygui as sc
title = "Simple_Calculator"
end_sign = 1#结束判断标志
result = 0#默认结果为0
sc.msgbox(msg='欢迎使用', title=title, image='calculator.gif', ok_button='开始')
# 欢迎界面 文件夹中要有calculator.gif命名的图片

while end_sign:
    first_number = sc.integerbox(msg='输入第一个数', title=title,lowerbound=-999999, upperbound=999999)
    if first_number is None:#判断要不要继续
        break

    operation = sc.buttonbox(msg='选择运算符号', title=title, choices=['+', '-', '*', '/'])
    if operation is None:#判断要不要继续
        break

    second_number = sc.integerbox(msg='输入第二个数', title=title,lowerbound=-999999, upperbound=999999)
    if second_number is None:#判断要不要继续
        break


    if operation == '+':
        result = first_number + second_number
    if operation == '-':
        result = first_number - second_number
    if operation == '*':
        result = first_number * second_number
    if operation == '/':
        result = first_number / second_number
    result = "计算结果:"+str(first_number)+operation+str(second_number)+'='+str(result)  #运算过程
    end_sign = sc.indexbox(msg=result, title=title, choices=['结束','继续'] )
    # 显示计算过程和结果结果,并判断要不要继续,点”结束“ end_sign赋值为0,退出程序。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Python 编程语言和 easygui 库来实现一个简单的五子棋游戏。下面是一个参考代码: ```python import easygui # 定义棋盘 chess_board = [[0] * 15 for i in range(15)] player = 1 # 玩家1为黑棋,玩家2为白棋 game_over = False # 游戏是否结束,默认为 False # 判断胜负的函数 def is_win(chess_board, row, col, player): count = 0 # 判断行 for i in range(15): if chess_board[row][i] == player: count += 1 if count == 5: return True else: count = 0 # 判断列 count = 0 for i in range(15): if chess_board[i][col] == player: count += 1 if count == 5: return True else: count = 0 # 判断正斜线 count = 0 for i in range(-4, 5): if row + i < 0 or row + i > 14 or col + i < 0 or col + i > 14: continue if chess_board[row + i][col + i] == player: count += 1 if count == 5: return True else: count = 0 # 判断反斜线 count = 0 for i in range(-4, 5): if row + i < 0 or row + i > 14 or col - i < 0 or col - i > 14: continue if chess_board[row + i][col - i] == player: count += 1 if count == 5: return True else: count = 0 return False # 显示棋盘 def show_chess_board(): for i in range(15): for j in range(15): if chess_board[i][j] == 0: easygui.button("", (i+1)*40, (j+1)*40, 40, 40, background_color="burlywood") elif chess_board[i][j] == 1: easygui.button("", (i+1)*40, (j+1)*40, 40, 40, background_color="black") elif chess_board[i][j] == 2: easygui.button("", (i+1)*40, (j+1)*40, 40, 40, background_color="white") # 执行游戏 while True: easygui.msgbox("当前是玩家" + str(player) + "的回合,请落子") msg = "请在棋盘上选择一个空位落子" title = "五子棋" choices = list(range(1, 16)) row = easygui.choicebox(msg, title, choices) - 1 col = easygui.choicebox(msg, title, choices) - 1 if chess_board[row][col] == 0: chess_board[row][col] = player show_chess_board() if is_win(chess_board, row, col, player): easygui.msgbox("玩家" + str(player) + "胜利!") game_over = True break if player == 1: player = 2 else: player = 1 else: easygui.msgbox("该位置已有棋子,请重新落子") # 判断平局 count = 0 for i in range(15): for j in range(15): if chess_board[i][j] != 0: count += 1 if count == 225: easygui.msgbox("平局!") game_over = True break ``` 这个代码使用 easygui 库来实现一个简单的五子棋游戏。首先定义一个棋盘(`chess_board`),然后通过交替落子来进行游戏,使用 `is_win` 函数来判断胜负,使用 `show_chess_board` 函数来显示当前的棋盘状态。 你可以直接复制这个代码并在 Python 环境中运行。注意,这个代码只是一个基本的实现,你可以根据自己的需要对其进行更改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值