Tkinter – 计算器
- 模拟系统的计算器功能
- 实现一个简单的具有加减法等操作的计算器
- 使用tkinter
- 操作步骤
- 画GUI
- 给每个控件配置相应的事件
- 写逻辑代码
# 第一步: 画出图形界面上部
from tkinter import *
root = Tk()
# 定义面板的大小
root.geometry('250x380')
root.title("北京图灵学院")
# 定义面板
# bg代表背景颜色(background), #dddddd是十六进制表示颜色的一个串
frame_show = Frame(width=300, height=150, bg='#dddddd')
frame_show.pack()
# 定义顶部区域
sv = StringVar()
sv.set('0')
# anchor:定义控件的锚点,e代表右边
# font代表字体
show_label = Label(frame_show, textvariable=sv, \
bg='green', width=12, height=1,\
font=("黑体", 20, 'bold'),\
justify=LEFT, anchor='e')
show_label.pack(padx=10, pady=10)
root.mainloop()
# 第二步: 画出图形界面下半部
from tkinter import *
root = Tk()
# 定义面板的大小
root.geometry('250x380')
root.title("北京图灵学院")
# 定义面板
# bg代表背景颜色(background), #dddddd是十六进制表示颜色的一个串
frame_show = Frame(width=300, height=150, bg='#dddddd')
frame_show.pack()
# 定义顶部区域
sv = StringVar()
sv.set('0')
# anchor:定义控件的锚点,e代表右边
# font代表字体
show_label = Label(frame_show, textvariable=sv, \
bg='green', width=12, height=1,\
font=("黑体", 20, 'bold'),\
justify=LEFT, anchor='e')
show_label.pack(padx=10, pady=10)
def delete():# 第二步: 画出图形界面下半部
from tkinter import *
root = Tk()
# 定义面板的大小
root.geometry('250x380')
root.title("北京图灵学院")
# 定义面板
# bg代表背景颜色(background), #dddddd是十六进制表示颜色的一个串
frame_show = Frame(width=300, height=150, bg='#dddddd')
frame_show.pack()
# 定义顶部区域
sv = StringVar()
sv.set('0')
# anchor:定义控件的锚点,e代表右边
# font代表字体
show_label = Label(frame_show, textvariable=sv, \
bg='green', width=12, height=1,\
font=("黑体", 20, 'bold'),\
justify=LEFT, anchor='e')
show_label.pack(padx=10, pady=10)
def delete():
print("我被删除了")
def fan():
print("烦了")
def clear():
print("科利尔")
# 按键区域
frame_bord = Frame(width=400, height=350, bg='#cccccc')
b_del = Button(frame_bord, text="←", width=5, height=1, \
command=delete)
b_del.grid(row=0, column=0)
button_clear = Button(frame_bord,text = 'C',width = 5,height =1,\
command = clear).grid(row = 0,column = 1)
button_fan = Button(frame_bord,text = '±',width = 5,height =1,\
command = fan).grid(row = 0,column = 2)
button_ce = Button(frame_bord,text = 'CE',width = 5,height =1,\
command = clear).grid(row = 0,column = 3)
def change(num):
print(num)
def operation(op):
print(op)
b_1 = Button(frame_bord, text='1', width=5, height=2,\
command=lambda:change("1"))
b_1.grid(row=1, column=0)
b_jia = Button(frame_bord, text='+', width=5, height=2,\
command=lambda:operation("+"))
b_jia.grid(row=2, column=0)
frame_bord.pack(padx=10, pady=10)
root.mainloop()
print("我被删除了")
def fan():
print("烦了")
def clear():
print("科利尔")
# 按键区域
frame_bord = Frame(width=400, height=350, bg='#cccccc')
b_del = Button(frame_bord, text="←", width=5, height=1, \
command=delete)
b_del.grid(row=0, column=0)
button_clear = Button(frame_bord,text = 'C',width = 5,height =1,\
command = clear).grid(row = 0,column = 1)
button_fan = Button(frame_bord,text = '±',width = 5,height =1,\
command = fan).grid(row = 0,column = 2)
button_ce = Button(frame_bord,text = 'CE',width = 5,height =1,\
command = clear).grid(row = 0,column = 3)
def change(num):
print(num)
def operation(op):
print(op)
b_1 = Button(frame_bord, text='1', width=5, height=2,\
command=lambda:change("1"))
b_1.grid(row=1, column=0)
b_jia = Button(frame_bord, text='+', width=5, height=2,\
command=lambda:operation("+"))
b_jia.grid(row=2, column=0)
frame_bord.pack(padx=10, pady=10)
root.mainloop()
+
#添加逻辑功能
# 第二步: 画出图形界面下半部
from tkinter import *
root = Tk()
# 定义面板的大小
root.geometry('250x380')
root.title("北京图灵学院")
# 定义面板
# bg代表背景颜色(background), #dddddd是十六进制表示颜色的一个串
frame_show = Frame(width=300, height=150, bg='#dddddd')
frame_show.pack()
# 定义顶部区域
sv = StringVar()
sv.set('0')
# anchor:定义控件的锚点,e代表右边
# font代表字体
show_label = Label(frame_show, textvariable=sv, \
bg='green', width=12, height=1,\
font=("黑体", 20, 'bold'),\
justify=LEFT, anchor='e')
show_label.pack(padx=10, pady=10)
def delete():
print("我被删除了")
def fan():
print("烦了")
def clear():
print("科利尔")
# 按键区域
frame_bord = Frame(width=400, height=350, bg='#cccccc')
b_del = Button(frame_bord, text="←", width=5, height=1, \
command=delete)
b_del.grid(row=0, column=0)
button_clear = Button(frame_bord,text = 'C',width = 5,height =1,\
command = clear).grid(row = 0,column = 1)
button_fan = Button(frame_bord,text = '±',width = 5,height =1,\
command = fan).grid(row = 0,column = 2)
button_ce = Button(frame_bord,text = 'CE',width = 5,height =1,\
command = clear).grid(row = 0,column = 3)
'''
考虑以下集中情况:
1. 按下数字
2. 按下操作符号
3,只考虑两个操作数操作,比考虑复杂情况
'''
num1 = ''
num2 = ''
operator = ''
def change(num):
'''
按下一个数字需要考虑两种情况:
1. 数字属于第一个操作数
2. 数字属于第二个操作数
3. 判断是否属于第一个操作数,可以通过operator判断
'''
# 加入操作数是None,表明肯定是第一个操作数
if not operator:
num1 = num1 + num
# 如果是第一个操作数,则只显示第一个操作数
sv.set(num1)
else:
num2 = num2 + num
# 如果是第二个操作数 ,则应该显示完整的计算式子
sv.set(num1+operator+num2)
def operation(op):
if op in ['+', '-', 'x', '/']:
operator = op
else: # 认为是按下的等于号
if op == "+":
rst = int(num1) + int(num2)
if op == "-":
rst = int(num1) + int(num2)
if op == "x":
rst = int(num1) + int(num2)
if op == "/":
rst = int(num1) + int(num2)
sv.set(str(rst))
b_1 = Button(frame_bord, text='1', width=5, height=2,\
command=lambda:change("1"))
b_1.grid(row=1, column=0)
b_jia = Button(frame_bord, text='+', width=5, height=2,\
command=lambda:operation("+"))
b_jia.grid(row=2, column=0)
frame_bord.pack(padx=10, pady=10)
root.mainloop()
正文完