|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是我编的计算器:
from tkinter import *
from math import *
window = Tk()
window.title='计算器'
window.geometry('300x450')
f=Frame(window)
f.pack()
v=StringVar()
def clear():
e.delete(0,END)
def calc():
v.set(eval(e.get()))
def pow1():
p=e.get()
result=int(p)*int(p)
v.set(result)
def factorial1():
s = 1
n=e.get()
for i in range(1,n+1):
s=s*i
v.set(s)
def sqrt1():
pass
e=Entry(f,text=v,width=8,font=('Arial',31))
e.grid(row=0,columnspan=4)
Button(f,text=1,width=2,command=lambda:e.insert(INSERT,'1'),font=('Arial',31)).grid(row=1,column=0)
Button(f,text=2,width=2,command=lambda:e.insert(INSERT,'2'),font=('Arial',31)).grid(row=1,column=1)
Button(f,text=3,width=2,command=lambda:e.insert(INSERT,'3'),font=('Arial',31)).grid(row=1,column=2)
Button(f,text='+',width=2,command=lambda:e.insert(INSERT,'+'),font=('Arial',31)).grid(row=1,column=3)
Button(f,text='4',width=2,command=lambda:e.insert(INSERT,'4'),font=('Arial',31)).grid(row=2,column=0)
Button(f,text='5',width=2,command=lambda:e.insert(INSERT,'5'),font=('Arial',31)).grid(row=2,column=1)
Button(f,text='6',width=2,command=lambda:e.insert(INSERT,'6'),font=('Arial',31)).grid(row=2,column=2)
Button(f,text='-',width=2,command=lambda:e.insert(INSERT,'-'),font=('Arial',31)).grid(row=2,column=3)
Button(f,text='7',width=2,command=lambda:e.insert(INSERT,'7'),font=('Arial',31)).grid(row=3,column=0)
Button(f,text='8',width=2,command=lambda:e.insert(INSERT,'8'),font=('Arial',31)).grid(row=3,column=1)
Button(f,text='9',width=2,command=lambda:e.insert(INSERT,'9'),font=('Arial',31)).grid(row=3,column=2)
Button(f,text='*',width=2,command=lambda:e.insert(INSERT,'*'),font=('Arial',31)).grid(row=3,column=3)
Button(f,text='0',width=2,command=lambda:e.insert(INSERT,'0'),font=('Arial',31)).grid(row=4,column=0)
Button(f,text='=',width=2,command=calc,font=('Arial',31)).grid(row=4,column=1)
Button(f,text='c',width=2,command=clear,font=('Arial',31)).grid(row=4,column=2)
Button(f,text='/',width=2,command=lambda:e.insert(INSERT,'/'),font=('Arial',31)).grid(row=4,column=3)
Button(f,text='p',width=2,command=pow1,font=('Arial',31)).grid(row=5,column=0)
Button(f,text='k',width=2,command=sqrt1,font=('Arial',31)).grid(row=5,column=1)
Button(f,text='j',width=2,command=factorial1,font=('Arial',31)).grid(row=5,column=2)
Button(f,text='.',width=2,command=lambda:e.insert(INSERT,'.'),font=('Arial',31)).grid(row=5,column=3)
def factorial1():
s = 1
n=e.get()
for i in range(1,n+1):
s=s*i
v.set(s)
为啥实现阶乘的函数运行错误:TypeError: can only concatenate str (not "int") to str;
还有这个开方函数要怎么弄
def sqrt1():
def factorial1():
s = 1
n=int(e.get())
while n:
s*=n
n-=1
v.set(s)
|
|