3条回答 默认 最新
- 辉煌仪奇 2021-11-23 14:20关注
有帮助请采纳
简单界面计算器import tkinter as tk class Calc(tk.Tk): """计算器窗体类""" def __init__(self): """初始化实例""" tk.Tk.__init__(self) self.title("计算器") self.memory = 0 # 暂存数值 self.create() def create(self): """创建界面""" btn_list = ["C", "M->", "->M", "/", "7", "8", "9", "*", "4", "5", "6", "-", "1", "2", "3", "+", "+/-", "0", ".", "="] r = 1 c = 0 for b in btn_list: self.button = tk.Button(self, text=b, width=5, command=(lambda x=b: self.click(x))) self.button.grid(row=r, column=c, padx=3, pady=6) c += 1 if c > 3: c = 0 r += 1 self.entry = tk.Entry(self, width=24, borderwidth=2, bg="yellow", font=("Consolas", 12)) self.entry.grid(row=0, column=0, columnspan=4, padx=8, pady=6) def click(self, key): """响应按钮""" if key == "=": # 输出结果 result = eval(self.entry.get()) self.entry.insert(tk.END, " = " + str(result)) elif key == "C": # 清空输入框 self.entry.delete(0, tk.END) elif key == "->M": # 存入数值 self.memory = self.entry.get() if "=" in self.memory: ix = self.memory.find("=") self.memory = self.memory[ix + 2:] self.title("M=" + self.memory) elif key == "M->": # 取出数值 if self.memory: self.entry.insert(tk.END, self.memory) elif key == "+/-": # 正负翻转 if "=" in self.entry.get(): self.entry.delete(0, tk.END) elif self.entry.get()[0] == "-": self.entry.delete(0) else: self.entry.insert(0, "-") else: # 其他键 if "=" in self.entry.get(): self.entry.delete(0, tk.END) self.entry.insert(tk.END, key) if __name__ == "__main__": Calc().mainloop()
简便计算器
def add(x, y): return x + y def substract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): return x / y if __name__ == '__main__': while True: print('''"*********欢迎来到简便计算器********* 请选择运算方式 1、相 加 2、相 减 3、相 乘 4、相 除 5、退 出 ''' ) choice = input("请输入你的选择(1/2/3/4/5):") if choice == "5": break num1 = eval(input("请输入第一个数字:")) num2 = eval(input("请输入第二个数字:")) if choice == '1': print('\t ', num1, '+', num2, '=', add(num1, num2)) elif choice == '2': print('\t ', num1, '-', num2, '=', substract(num1, num2)) elif choice == '3': print('\t ', num1, '*', num2, '=', multiply(num1, num2)) elif choice == '4': print('\t ', num1, '/', num2, '=', divide(num1, num2)) else: print("输入错误,请重新输入") print('*' * 40)
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 乌班图ip地址配置及远程SSH
- ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
- ¥15 PSPICE制作一个加法器
- ¥15 javaweb项目无法正常跳转
- ¥15 VMBox虚拟机无法访问
- ¥15 skd显示找不到头文件
- ¥15 机器视觉中图片中长度与真实长度的关系
- ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
- ¥15 java 的protected权限 ,问题在注释里
- ¥15 这个是哪里有问题啊?