python通过tkinter界面库实现三角形成立的测试
from tkinter import *
from tkinter import messagebox
login = Tk()
login.title('验证')
login.geometry('800x600')
Label(login,text='实现三角形成立的验证').grid(row=0,column=0,columnspan=2)
Label(login,text='边a:').grid(row=1,column=0)
Label(login,text='边b:').grid(row=2,column=0)
Label(login,text='边c:').grid(row=3,column=0)
name = Entry(login)
name.grid(row=1,column=1)
name1 = Entry(login)
name1.grid(row=2,column=1)
name2 = Entry(login)
name2.grid(row=3,column=1)def verity(names):flag=Trueif '.' in names:#浮点数验证if names[0]=='-':flag=Falseelse:names=names.replace('.','')for s in names:if ord(s)<48 or ord(s)>57: #0-9flag=Falsebreakelse:for s in names:if ord(s)<48 or ord(s)>57: #0-9flag=Falsebreakreturn flagdef validate():name11=name.get()name12 = name1.get()name13 = name2.get()print(name11,name12,name13)#1负数、0和字符验证if name11!='' and name12!='' and name13!='' and verity(name11) and verity(name12) and verity(name13) and name11[0]!='0'and name12[0]!='0'and name13[0]!='0':messagebox.showinfo(title='success', message='输入边长合格')# 2三角形成立的条件if float(name11) + float(name12) > float(name13) and float(name11) + float(name13) > float(name12) and float(name13) + float(name12) > float(name11):messagebox.showinfo(title='success', message='符合三角形成立条件')if float(name11) ** 2 + float(name12) ** 2 == float(name13) ** 2 or float(name11) ** 2 + float(name13) ** 2 == float(name12) ** 2 or float(name13) ** 2 + float(name12) ** 2 == float(name11) ** 2:messagebox.showinfo(title='success', message='该三角形是直角三角形')elif float(name11) == float(name12) == float(name13) or float(name11) == float(name13) == float(name12) or float(name13) == float(name12) == float(name11):messagebox.showinfo(title='success', message='该三角形是等边三角形')elif float(name11) == float(name12) or float(name11) == float(name13) or float(name13) == float(name12):messagebox.showinfo(title='success', message='该三角形是等腰三角形')else:messagebox.showinfo(title='success', message='该三角形是普通三角形')else:messagebox.showerror(title='wrong', message='不符合三角形成立条件')else:messagebox.showerror(title='wrong', message='输入边长不合格')def main():Button(login, text='测试', command=validate).grid(row=4, column=0, columnspan=2)login.mainloop()if __name__ == '__main__':main()
运行结果:
注:本文中存在的不足,请在留言区批评指正,谢谢!