Python用Tkinter实现圆的半径 面积 周长 知一求二程序
import tkinter as tk
from tkinter import messagebox
from tkinter import *
app = tk.Tk()
app.title('圆的半径 面积 周长 知一求二程序')
app.geometry('425x125')
label1 = tk.Label(app, text="半径")
label2 = tk.Label(app, text="面积")
label3 = tk.Label(app, text="周长")
label1.grid(row=0)
label2.grid(row=1)
label3.grid(row=2)
entryx=Entry(app,font=("宋体",20))
entryy=Entry(app,font=("宋体",20))
entryz=Entry(app,font=("宋体",20))
entryx.grid(row=0,column=1)
entryy.grid(row=1,column=1)
entryz.grid(row=2,column=1)
import math
class ACricle(object):
def __init__(self,r):
self.r = r
def get_area(self):
return math.pi * math.pow(self.r, 2)
def get_perimeter(self):
return 2 * math.pi * self.r
class BCricle(object):
def __init__(self,s):
self.s = s
def get_radius(self):
return math.pow((self.s/math.pi),0.5)
def get_2perimeter(self):
return math.pow((self.s/math.pi),0.5)*2*math.pi
class CCricle(object):
def __init__(self,p):
self.p = p
def get_2radius(self):
return self.p/(2*math.pi)
def get_2area(self):
return math.pow((self.p/(2*math.pi)),2)*math.pi
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
def ok1():
if is_number(entryx.get()):
r = float(entryx.get())
if r<=0 :
messagebox.showinfo(title='提示信息', message=u'您的输入无效!')
else:
a = ACricle(r)
messagebox.showinfo(title='面积与周长', message=f'圆面积:{a.get_area()}圆周长:{a.get_perimeter()}')
else:
messagebox.showinfo("提示", "您输入有误!")
return
app.destroy()
def ok2():
if is_number(entryx.get()):
s = float(entryy.get())
if s<=0 :
messagebox.showinfo(title='提示信息', message=u'您的输入无效!')
else:
b = BCricle(s)
messagebox.showinfo(title='半径与周长', message=f'圆半径:{b.get_radius()}圆周长:{b.get_2perimeter()}')
else:
messagebox.showinfo("提示", "您输入有误!")
return
app.destroy()
def ok3():
if is_number(entryx.get()):
p = float(entryz.get())
if p<=0 :
messagebox.showinfo(title='提示信息', message=u'您的输入无效!')
else:
c = CCricle(p)
messagebox.showinfo(title='半径与面积', message=f'圆半径:{c.get_2radius()}圆面积:{c.get_2area()}')
else:
messagebox.showinfo("提示", "您输入有误!")
return
app.destroy()
b1 = tk.Button(app,text='确定',width=10,command=ok1)
b1.grid(row=0,column=2)
b2 = tk.Button(app,text='确定',width=10,command=ok2)
b2.grid(row=1,column=2)
b3 = tk.Button(app,text='确定',width=10,command=ok3)
b3.grid(row=2,column=2)
app.mainloop()