基于tkinter的简易加减乘除计算器
Posted 只是有点小怂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于tkinter的简易加减乘除计算器相关的知识,希望对你有一定的参考价值。
from tkinter import *
from tkinter import ttk
# 整个窗体的GUI是一个类
class Calculator(Tk):
def __init__(self):
self.frame = Tk()
self.frame.title("计算器")
self.frame.geometry("700x200+300+250")
self.frame.resizable() # 不允许调整大小
self.frame["bg"] = "darkgray" # 窗体背景色
# 添加一个文本输入框 数字1
self.var_num01 = StringVar()
self.Entry_num01 = Entry(self.frame, textvariable=self.var_num01, font=("微软雅黑", 14, "bold"), width=10)
self.Entry_num01.place(x=20, y=80)
# 添加一个下拉框的运算符
self.var_action = StringVar()
self.ComboBox_action = ttk.Combobox(self.frame, textvariable=self.var_action, font=("微软雅黑", 14, "bold"), width=4)
self.ComboBox_action["values"] = ["加[+]","减[-]","乘[*]","除[/]","余[%]"]
self.ComboBox_action["state"] = "readonly" # 不许修改
self.ComboBox_action.current(0) # 默认为加
self.ComboBox_action.place(x=170, y=80)
# 添加一个文本输入框 数字2
self.var_num02 = StringVar()
self.Entry_num02 = Entry(self.frame, textvariable=self.var_num02, font=("微软雅黑", 14, "bold"), width=10)
self.Entry_num02.place(x=265, y=80)
# 添加一个Label标签
self.Label_result = Label(self.frame, text="=",font=("微软雅黑", 14, "bold"), width=2)
self.Label_result.place(x=400, y=80)
# 添加一个结果文本框
self.var_result = StringVar()
self.Entry_result = Entry(self.frame, textvariable=self.var_result, state="readonly",font=("微软雅黑", 14, "bold"), width=10)
self.Entry_result.place(x=450, y=80)
# 显示计算按钮
self.Button_Cal = Button(self.frame, text="计算",width=6, command=self.number_cal)
self.Button_Cal.place(x=600, y=80)
def show(self):
self.frame.mainloop()
def number_cal(self):
num01 = self.var_num01.get()
num02 = self.var_num02.get()
action = self.var_action.get()
if "+" in action:
self.var_result.set(str(int(num01) + int(num02)))
elif "-" in action:
self.var_result.set(str(int(num01) - int(num02)))
elif "*" in action:
self.var_result.set(str(int(num01) * int(num02)))
elif "/" in action:
self.var_result.set(str(int(num01) / int(num02)))
elif "%" in action:
self.var_result.set(str(int(num01) % int(num02)))
else:
pass
if __name__ == "__main__":
my_calc = Calculator()
my_calc.show()
以上是关于基于tkinter的简易加减乘除计算器的主要内容,如果未能解决你的问题,请参考以下文章
Python基于tkinter.messagebox实现简易消息框对话框
java代码实例 使用switch实现简易的计算器(实现加减乘除)