刚学python2个月,随便写的计算器(附exe)
本帖最后由 kuank 于 2023-11-26 12:31 编辑rt效果
```
import tkinter as tk
from tkinter import messagebox
class Calculator:
def __init__(self, master):
self.master = master
self.master.title("简单计算器")
self.master.geometry("250x400")# 调整窗口大小
# 输入框
self.entry = tk.Entry(master, width=20, font=('Arial', 15))# 调整输入框大小
self.entry.grid(row=0, column=0, columnspan=10, pady=10)# 调整行和列的距离
# 按钮
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+',
'退格', 'C', '(', ')', '关于'
]
row_val = 1
col_val = 0
for button in buttons:
if button == '关于':
tk.Button(master, text=button, width=6, height=2, command=self.show_about_popup).grid(row=row_val, column=col_val, padx=5, pady=5)
else:
tk.Button(master, text=button, width=6, height=2, command=lambda b=button: self.on_button_click(b)).grid(row=row_val, column=col_val, padx=5, pady=5)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
def on_button_click(self, button):
current_entry = self.entry.get()
if button == '=':
try:
result = eval(current_entry)
self.entry.delete(0, tk.END)
self.entry.insert(tk.END, str(result))
except Exception as e:
self.entry.delete(0, tk.END)
self.entry.insert(tk.END, "错误")
elif button == '退格':
current_entry = current_entry[:-1]
self.entry.delete(0, tk.END)
self.entry.insert(tk.END, current_entry)
elif button == 'C':
self.entry.delete(0, tk.END)
else:
self.entry.insert(tk.END, button)
def show_about_popup(self):
messagebox.showinfo("关于", "test")#关于页面的文字
if __name__ == "__main__":
root = tk.Tk()
calculator = Calculator(root)
root.mainloop()
```
https://wwqu.lanzouq.com/iUwqr1fygtwd 密码:52pj 感谢分享,学习下。 二个月学的不错啊 学的那个视频啊 厉害,我两个月都没这成果 楼主能加个功能 计算余数的吗?比如500/320=1,180 这样的。{:1_893:} 人生苦短,要用python 从入门到放弃,python是为何物~~~~~ Python是个不错的工具,感谢分享案例,共同进步
Python是个不错的工具,感谢分享案例,共同进步