基于tkinter模块,制作的简易计算器
import tkinter as tk
# 显示和实际计算是不同的,得分两套来
win = tk.Tk()
win.title("calculator")
win_width = 400
win_heigh = 560
# screen_x = 0.5 * win.winfo_screenwidth() #tkinter测定当前屏幕尺寸的命令
# screen_y = 0.5 * win.winfo_screenheight()
win_x = 0.5 * (win.winfo_screenwidth() - win_width)
win_y = 0.4 * (win.winfo_screenheight() - win_heigh)
win.geometry("%dx%d+%d+%d" % (win_width, win_heigh, win_x, win_y)) # 设置窗口尺寸、位置,win_x、win_y为窗口左上角距离屏幕左、上边界距离
judge = 0 # 判定参数,每次按下等于键后参数归0,按下非等于键参数变为1
def press_num(num):
global judge
if judge == 1:
var.set('')
result.set('')
old_num = var.get()
if old_num == '0':
var.set(num)
else:
var.set(old_num + num)
old_result = result.get()
if old_result == '0':
result.set(num)
else:
result.set(old_result + num)
judge = 0
win.update()
def press_function_1(func):
global judge
if judge == 1:
var.set(result_1.get())
result.set(result_1.get())
old_func = var.get()
if old_func == '0':
var.set(func)
else:
if func == '+':
var.set(old_func + func)
win.update()
if func == '-':
var.set(old_func + func)
win.update()
if func == '÷':
var.set(old_func + func)
win.update()
if func == '×':
var.set(old_