标签:作者 add 个数 负数 justify cti odi imp layout
import tkinter
import math
class Deom:
operation_sign = False
sign1 = False
sign2 = False
list1 = []
list2 = []
def __init__(self):
self.root = tkinter.Tk()
self.root.minsize(320, 400)
self.root.title(‘什么都算不对牌计算器‘)
self.layout()
self.root.mainloop()
def layout(self):
self.val0 = tkinter.StringVar()
self.val0.set(‘0‘)
self.val = tkinter.StringVar()
self.val.set(‘0‘) # 文本设置
#标题
def newwindow():
# 相当于主界面
tp = tkinter.Toplevel(width=300, height=330, bg=‘white‘)
# 禁止调整大小
#tp.resizable(width=False, height=False)
# 定义标题
tp.title(‘help‘)
self.val3 = tkinter.StringVar()
self.val3.set(‘实现功能:\n简易计算器,加减乘除,开方,平方,百分数,分数,正负\n数,clear error ,clear,delete\n解决bug:\n1. 一直点击运算符号以及等号会连加\n2. 除数为零提示除数不能为0\n3. 小数结果如果大于15位,则保留15位,整数则提示超\n出范围\n4. 小数点控制为一个,先按小数点会先出零\n5. 已修复目前已知bug\n\n\ntips:\n若发现bug请及时联系作者修改!!\n本界面趋于原生\n\n版权所有@赵晗python04‘)
# 消息记录
label3 = tkinter.Label(tp, bg = ‘white‘,textvariable = self.val3,anchor = ‘nw‘,justify = ‘left‘)
label3.place(x = 0, y = 0)
# 创建总菜单
menubar = tkinter.Menu(self.root,bg = ‘#F2F2F2‘,activeforeground = ‘white‘,activebackground = ‘#1717FF‘)
# 创建一个下拉菜单,并且加入文件菜单
filemenu = tkinter.Menu(menubar,bg = ‘#F2F2F2‘,activeforeground = ‘white‘,activebackground = ‘#1717FF‘)
# 创建下来菜单的选项
filemenu.add_command(label="about",command=newwindow)
# 创建下拉菜单的分割线
filemenu.add_separator()
filemenu.add_command(label="Exit", command=self.root.quit)
# 将文件菜单作为下拉菜单添加到总菜单中,并且将命名为File
menubar.add_cascade(label="help", menu=filemenu)
# 显示总菜单
self.root.config(menu=menubar)
# 标签
label0 = tkinter.Label(self.root,textvariable=self.val0, bg=‘white‘, fg = ‘#7B7B7B‘,font=(‘黑体‘, 15), width=‘15‘, anchor=‘e‘,borderwidth=‘10‘).place(x=‘0‘, y=‘0‘,width= 320,height = 40)
# 按键)
label1 = tkinter.Label(self.root, textvariable=self.val, bg=‘white‘, font=(‘黑体‘, 25), width=‘15‘, anchor=‘e‘,borderwidth=‘10‘).place(x=‘0‘, y=‘40‘,width = 320,height = 60)
# 按键
btn9 = tkinter.Button(self.root, text=‘9‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘, command=lambda :self.num(‘9‘)).place(x=160, y=200, width=80, height=50)
btn8 = tkinter.Button(self.root, text=‘8‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.num(‘8‘)).place(x=80, y=200, width=80, height=50)
btn7 = tkinter.Button(self.root, text=‘7‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.num(‘7‘)).place(x=0, y=200, width=80, height=50)
btn6 = tkinter.Button(self.root, text=‘6‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.num(‘6‘)).place(x=160, y=250, width=80, height=50)
btn5 = tkinter.Button(self.root, text=‘5‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.num(‘5‘)).place(x=80, y=250, width=80, height=50)
btn4 = tkinter.Button(self.root, text=‘4‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘, command=lambda :self.num(‘4‘)).place(x=0, y=250, width=80, height=50)
btn3 = tkinter.Button(self.root, text=‘3‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘, command=lambda :self.num(‘3‘)).place(x=160, y=300, width=80, height=50)
btn2 = tkinter.Button(self.root, text=‘2‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘, command=lambda :self.num(‘2‘)).place(x=80, y=300, width=80, height=50)
btn1 = tkinter.Button(self.root, text=‘1‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘, command=lambda :self.num(‘1‘)).place(x=0, y=300, width=80, height=50)
btn0 = tkinter.Button(self.root, text=‘0‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘, command=lambda :self.num(‘0‘)).place(x=80, y=350, width=80, height=50)
btnsign = tkinter.Button(self.root, text=‘±‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘,command = self.plus_minus).place(x=0, y=350, width=80, height=50)
btnpoint = tkinter.Button(self.root, text=‘.‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.num(‘.‘)).place(x=160, y=350, width=80, height=50)
btndiv = tkinter.Button(self.root, text=‘÷‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.operation(‘/‘)).place(x=240, y=150, width=80, height=50)
btnmul = tkinter.Button(self.root, text=‘ב, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.operation(‘*‘)).place(x=240, y=200, width=80, height=50)
btnadd = tkinter.Button(self.root, text=‘+‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘, command=lambda :self.operation(‘+‘)).place(x=240, y=300, width=80, height=50)
btnsub = tkinter.Button(self.root, text=‘-‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.operation(‘-‘)).place(x=240, y=250, width=80, height=50)
btnequal = tkinter.Button(self.root, text=‘=‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activeforeground = ‘white‘,activebackground = ‘#1717FF‘, command=self.eq_operation).place(x=240, y=350, width=80, height=50)
btndel = tkinter.Button(self.root, text=‘del‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘,command = self.delete_operation).place(x=160, y=150, width=80, height=50)
btnclr = tkinter.Button(self.root, text=‘c‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activeforeground = ‘white‘,activebackground = ‘#FE524E‘,command = self.clear_all).place(x=80, y=150, width=80, height=50)
btnce = tkinter.Button(self.root, text=‘ce‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘,command = self.clear_error).place(x=0, y=150, width=80, height=50)
btnpercent = tkinter.Button(self.root, text=‘%‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘,command = self.percent_operation).place(x=0, y=100, width=80, height=50)
btnevolution = tkinter.Button(self.root, text=‘√‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘,command =self.evolution_operation).place(x=80, y=100, width=80, height=50)
btnsquare = tkinter.Button(self.root, text=‘x2‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘,command = self.square_operation).place(x=160, y=100, width=80, height=50)
btngrade = tkinter.Button(self.root, text=‘1/x‘, bg = ‘#F2F2F2‘,font=(‘黑体‘, 15),activebackground = ‘#CFEFFE‘,command = self.grade_operation).place(x=240, y=100, width=80, height=50)
#键盘输入操作
def num(self,number):
a = number
print(number)
‘‘‘if self.sign2 == True:
if number == ‘0‘:
print(‘error‘)‘‘‘
if self.operation_sign == True:
self.val.set(‘0‘)
self.operation_sign = False
if self.val.get() == ‘0‘: # 如果屏幕中数字为0,即一开始的默认值,判断,是0则变为按下的数字不是0,进行拼接
self.val.set(number)
if number == ‘.‘:
self.val.set(‘0‘ + number)
else: #不是0的情况
if self.val.get().count(‘.‘) == 0: #如果获取的字符串里面只有一个.
self.val.set(self.val.get() + number) # 屏幕所得到的数字 字符串连接下一次按入的数字
elif number != ‘.‘:
self.val.set(self.val.get() + number)
‘‘‘if self.sign2 == True:
if self.val.get() == ‘0‘:
print(‘error‘)‘‘‘
#self.sign2 = False
self.sign1 = True
self.sign2 = True
# 正负操作
def plus_minus(self):
list2 = self.val.get()
if list2[0] == ‘-‘:
self.val.set(list2[1:])
elif list2[0] != ‘-‘ and list2 != ‘0‘:
self.val.set(‘-‘ + list2)
self.sign1 = True
#退格操作
def delete_operation(self):
if len(self.val.get()) >1:
self.list1 = list(self.val.get())
self.list1.pop()
result = ‘‘.join(self.list1)
self.val.set(result)
else:
self.val.set(‘0‘)
#平方操作
def square_operation(self):
self.val.get()
self.list1.append(self.val.get())
list2 = ‘** 2‘
self.list1.append(list2)
str1 = ‘‘.join(self.list1) # 转换为字符串!
print(str1)
self.val0.set(str1) # 副框显示字符串也可以是列表
result = eval(str1) # 进行运算,python 代码
if result > 1:
result1 = str(result)
if len(result1) > 17:
self.val.set(‘结果超过范围‘)
else:
self.val.set(result)
else:
result1 = round(result, 15)
self.val.set(result1)
print(self.list1)
self.list1.clear()
self.operation_sign = True
#开平方操作
def evolution_operation(self):
try:
self.val.get()
list2 = ‘math.sqrt({0})‘.format(self.val.get())
print(list2)
self.list1.append(list2)
str1= ‘‘.join(self.list1)
print(str1)
self.val.set(eval(str1))
result = eval(str1) # 进行运算,python 代码
result1 = round(result, 15)
self.val.set(result1)
self.list1.clear()
self.operation_sign = True
except ValueError:
self.val.set(‘不能对负数开根号‘)
#%操作
def percent_operation(self):
self.val.get()
self.list1.append(self.val.get())
list2 = ‘/ 100‘
self.list1.append(list2)
result = ‘‘.join(self.list1)
self.val.set(eval(result))
print(self.list1)
self.list1.clear()
self.operation_sign = True
#分数
def grade_operation(self):
try:
self.val.get()
print(self.list1)
list2 = ‘1/{}‘.format(self.val.get())
self.list1.append(list2)
str1= ‘‘.join(self.list1)
print(str1)
self.val.set(eval(str1))
result = eval(str1) # 进行运算,python 代码
result1 = round(result, 15)
self.val.set(result1)
print(self.list1)
self.list1.clear()
self.operation_sign = True
except ZeroDivisionError:
self.val.set(‘除数不能为零‘)
#清空全部数据操作
def clear_all(self):
self.val.set(‘0‘)
self.list1 = []
self.val0.set(‘0‘)
self.operation_sign = False
#清空错误操作
def clear_error(self):
if self.val.get() != ‘‘:
self.val.set(‘0‘)
#运算操作
def operation(self,addnum):
if self.sign1 == True: #满足这个条件即可按下运算键,否则不可以,即0-9与等号
print(addnum) # addnum代表符号
print(self.val.get()) # 获取文本 字符串格式
self.list1.append(self.val.get()) # 将文本追加到list1中,此时val.get为文本中内容 列表
self.list1.append(addnum) # 【2 + 】
print(self.list1) # 需要判断运算符号是否按下
self.val0.set(self.list1)
self.operation_sign = True
self.sign1 = False
#等号操作
def eq_operation(self):
try:
if self.sign2 == True:
if self.sign1 == True: #sign1 为true 可以等于
self.list1.append(self.val.get()) # 按下等号之后将之前的一个数值传入list1
print(self.list1)
str1 = ‘‘.join(self.list1) # 转换为字符串!
print(str1)
self.val0.set(str1) # 副框显示字符串也可以是列表
result = eval(str1) # 进行运算,python 代码
if result > 1:
result1 = str(result)
if len(result1) > 15:
self.val.set(‘结果超过范围‘)
else:
self.val.set(result)
else:
result1 = round(result,15)
self.val.set(result1)
self.list1.clear()
self.operation_sign = True #此时判断出按键是否按下
self.sign1 = True #按下等号 变为true 数字可以按下
self.sign2 = False
except ZeroDivisionError:
self.val.set(‘除数不能为零‘)
deom = Deom()
标签:作者 add 个数 负数 justify cti odi imp layout
原文地址:http://www.cnblogs.com/zxf123/p/7906631.html