Python 用tkinter写一个计算器
# -*- coding: utf-8 -*-
"""
 * @Date : 2020-09-29 15:43
 * @Auth : xiaoshuai.zhu
 * @File :计算器.py
 * @IDE :PyCharm
 * @Version 1.0
"""
import tkinter
import tkinter.messagebox
from tkinter import *
from tkinter.messagebox import *
import tkinter.font as tkfont
class Calculator:
    def __init__(self):
        self.frame = tkinter.Toplevel(root)
        self.Button_call = Button(self.frame, text="计算", command=self.get_result1)
        self.Button_call.place(x=500, y=0)
        self.Button_call2 = Button(self.frame, text="计算", command=self.get_result2)
        self.Button_call2.place(x=500, y=60)
        self.Button_call3 = Button(self.frame, text="计算", command=self.get_result3)
        self.Button_call3.place(x=500, y=120)
        self.Button_call4 = Button(self.frame, text="计算", command=self.get_result4)
        self.Button_call4.place(x=500, y=180)
        self.frame.title("计算面积")
        print("点击了面积按钮")
        self.frame.geometry("600x250+700+10")
        self.frame.resizable(0, 0)
        self.frame["bg"] = "lightgray"

        # 添加一个Label 标签
        self.Label_input = Label(self.frame, text="请输入圆的半径", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_input.place(x=0, y=0)

        self.Label_input2 = Label(self.frame, text="请输入三角形三边", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_input2.place(x=0, y=60)

        self.Label_input3 = Label(self.frame, text="请输入长方形长宽", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_input3.place(x=0, y=120)

        self.Label_input4 = Label(self.frame, text="请输入梯形底和高", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_input4.place(x=0, y=180)

        # 添加输入的文本框
        self.var_radius = StringVar()
        self.Entry_input = Entry(self.frame, textvariable=self.var_radius, font=("微软雅黑", 12, "bold"), width=30)
        self.Entry_input.place(x=130, y=0)
        self.Label_area = Label(self.frame, text="圆的面积", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area.place(x=0, y=30)

        self.var_radius21 = StringVar()
        self.var_radius22 = StringVar()
        self.var_radius23 = StringVar()
        self.Entry_input2 = Entry(self.frame, textvariable=self.var_radius21, font=("微软雅黑", 12, "bold"), width=10)
        self.Entry_input2.place(x=130, y=60)
        self.Entry_input2 = Entry(self.frame, textvariable=self.var_radius22, font=("微软雅黑", 12, "bold"), width=10)
        self.Entry_input2.place(x=260, y=60)
        self.Entry_input2 = Entry(self.frame, textvariable=self.var_radius23, font=("微软雅黑", 12, "bold"), width=10)
        self.Entry_input2.place(x=390, y=60)
        self.Label_area2 = Label(self.frame, text="三角形的面积", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area2.place(x=0, y=90)

        self.var_radius31 = StringVar()
        self.var_radius32 = StringVar()
        self.Entry_input3 = Entry(self.frame, textvariable=self.var_radius31, font=("微软雅黑", 12, "bold"), width=10)
        self.Entry_input3.place(x=130, y=120)
        self.Entry_input3 = Entry(self.frame, textvariable=self.var_radius32, font=("微软雅黑", 12, "bold"), width=10)
        self.Entry_input3.place(x=260, y=120)
        self.Label_area3 = Label(self.frame, text="长方形的面积", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area3.place(x=0, y=150)

        self.var_radius41 = StringVar()
        self.var_radius42 = StringVar()
        self.var_radius43 = StringVar()
        self.Entry_input4 = Entry(self.frame, textvariable=self.var_radius41, font=("微软雅黑", 12, "bold"), width=10)
        self.Entry_input4.place(x=130, y=180)
        self.Entry_input4 = Entry(self.frame, textvariable=self.var_radius42, font=("微软雅黑", 12, "bold"), width=10)
        self.Entry_input4.place(x=260, y=180)
        self.Entry_input4 = Entry(self.frame, textvariable=self.var_radius43, font=("微软雅黑", 12, "bold"), width=10)
        self.Entry_input4.place(x=390, y=180)
        self.Label_area4 = Label(self.frame, text="梯形的面积", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area4.place(x=0, y=210)

        self.var_area = StringVar()
        self.Label_area_result = Label(self.frame, textvariable=self.var_area, text="圆的面积的结果", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area_result.place(x=100, y=30)

        self.var_area2 = StringVar()
        self.Label_area_result2 = Label(self.frame, textvariable=self.var_area2, text="三角形的面积的结果", background="lightgray",foreground="blue", font=("微软雅黑", 12, "bold"))
        self.Label_area_result2.place(x=100, y=90)

        self.var_area3 = StringVar()
        self.Label_area_result3 = Label(self.frame, textvariable=self.var_area3, text="长方形的面积的结果",background="lightgray", foreground="blue", font=("微软雅黑", 12, "bold"))
        self.Label_area_result3.place(x=100, y=150)

        self.var_area4 = StringVar()
        self.Label_area_result4 = Label(self.frame, textvariable=self.var_area4, text="梯形的面积的结果",background="lightgray", foreground="blue", font=("微软雅黑", 12, "bold"))
        self.Label_area_result4.place(x=100, y=210)
    def check_input(self, radius: str):
        if radius=='':
            print("你输入为空")
        if radius.replace(".", "").isdigit():
            return True
        else:
            return False
    def get_area1(self, radius: float):
        PI = 3.1415926
        circle_area = PI * radius * radius
        return circle_area
    def get_area2(self, radius21: float,radius22: float,radius23: float):
        P=(radius21+radius22+radius23)/2
        circle_area2 = pow(P*(P-radius21)*(P-radius22)*(P-radius23),0.5)
        return circle_area2
    def get_area3(self, radius31: float, radius32: float):
        circle_area3 = radius31*radius32
        return circle_area3
    def get_area4(self, radius41: float, radius42: float, radius43: float):
        circle_area4 = (radius41+radius42)*radius43/2
        return circle_area4
    def get_result1(self):
        print("点击了面积计算按钮1")
        radius = self.var_radius.get()
        if self.check_input(radius):
            circle_area = self.get_area1(float(radius))
            self.var_area.set("%.2f" % circle_area)
        else:
            showinfo("系统消息", "圆的半径 不符合要求")
    def get_result2(self):
        print("点击了面积计算按钮2")
        radius21 = self.var_radius21.get()
        radius22 = self.var_radius22.get()
        radius23 = self.var_radius23.get()
        if self.check_input(radius21) and self.check_input(radius22) and self.check_input(radius23):
            circle_area2 = self.get_area2(float(radius21),float(radius22),float(radius23))
            self.var_area2.set("%.2f" % circle_area2)
        else:
            showinfo("系统消息", "三角形三边 不符合要求")
    def get_result3(self):
        print("点击了面积计算按钮3")
        radius31 = self.var_radius31.get()
        radius32 = self.var_radius32.get()
        if self.check_input(radius31) and self.check_input(radius32):
            circle_area3 = self.get_area3(float(radius31), float(radius32))
            self.var_area3.set("%.2f" % circle_area3)
        else:
            showinfo("系统消息", "长方形两边 不符合要求")
    def get_result4(self):
        print("点击了面积计算按钮4")
        radius41 = self.var_radius41.get()
        radius42 = self.var_radius42.get()
        radius43 = self.var_radius43.get()
        if self.check_input(radius41) and self.check_input(radius42) and self.check_input(radius43):
            circle_area4 = self.get_area4(float(radius41), float(radius42), float(radius43))
            self.var_area4.set("%.2f" % circle_area4)
        else:
            showinfo("系统消息", "梯形三边 不符合要求")
    def show_a(self):
         self.frame.mainloop()

class Calculator2:
    def __init__(self):
        self.frame = tkinter.Toplevel(root)
        self.Button_call = Button(self.frame, text="计算", command=self.get_result1)
        self.Button_call.place(x=500, y=0)
        self.Button_call2 = Button(self.frame, text="计算", command=self.get_result2)
        self.Button_call2.place(x=500, y=60)
        self.Button_call3 = Button(self.frame, text="计算", command=self.get_result3)
        self.Button_call3.place(x=500, y=120)
        self.Button_call4 = Button(self.frame, text="计算", command=self.get_result4)
        self.Button_call4.place(x=500, y=180)
        self.frame.title("计算体积")
        print("点击了体积按钮")
        self.frame.geometry("600x250+700+200")
        self.frame.resizable(0, 0)
        self.frame["bg"] = "lightgray"

        # 添加一个Label 标签
        self.Label_input = Label(self.frame, text="请输入球的半径", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_input.place(x=0, y=0)

        self.Label_input2 = Label(self.frame, text="输入长方体三边", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_input2.place(x=0, y=60)

        self.Label_input3 = Label(self.frame, text="输入圆柱半径高", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_input3.place(x=0, y=120)

        self.Label_input4 = Label(self.frame, text="输入圆锥半径高", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_input4.place(x=0, y=180)

        # 添加输入的文本框
        self.var_radius = StringVar()
        self.Entry_input = Entry(self.frame, textvariable=self.var_radius, font=("微软雅黑", 12, "bold"), width=30)
        self.Entry_input.place(x=130, y=0)
        self.Label_area = Label(self.frame, text="球的体积", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area.place(x=0, y=30)

        self.var_radius21 = StringVar()
        self.var_radius22 = StringVar()
        self.var_radius23 = StringVar()
        self.Entry_input2 = Entry(self.frame, textvariable=self.var_radius21, font=("微软雅黑", 12, "bold"), width=10)
        self.Entry_input2.place(x=130, y=60)
        self.Entry_input2 = Entry(self.frame, textvariable=self.var_radius22, font=("微软雅黑", 12, "bold"), width=10)
        self.Entry_input2.place(x=260, y=60)
        self.Entry_input2 = Entry(self.frame, textvariable=self.var_radius23, font=("微软雅黑", 12, "bold"), width=10)
        self.Entry_input2.place(x=390, y=60)
        self.Label_area2 = Label(self.frame, text="长方体的体积", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area2.place(x=0, y=90)

        self.var_radius31 = StringVar()
        self.var_radius32 = StringVar()
        self.Entry_input3 = Entry(self.frame, textvariable=self.var_radius31, font=("微软雅黑", 12, "bold"), width=10)
        self.Entry_input3.place(x=130, y=120)
        self.Entry_input3 = Entry(self.frame, textvariable=self.var_radius32, font=("微软雅黑", 12, "bold"), width=10)
        self.Entry_input3.place(x=260, y=120)
        self.Label_area3 = Label(self.frame, text="圆柱的体积", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area3.place(x=0, y=150)

        self.var_radius41 = StringVar()
        self.var_radius42 = StringVar()
        self.Entry_input4 = Entry(self.frame, textvariable=self.var_radius41, font=("微软雅黑", 12, "bold"), width=10)
        self.Entry_input4.place(x=130, y=180)
        self.Entry_input4 = Entry(self.frame, textvariable=self.var_radius42, font=("微软雅黑", 12, "bold"), width=10)
        self.Entry_input4.place(x=260, y=180)
        self.Label_area4 = Label(self.frame, text="圆锥的体积", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area4.place(x=0, y=210)

        self.var_area = StringVar()
        self.Label_area_result = Label(self.frame, textvariable=self.var_area, text="球的体积的结果", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area_result.place(x=100, y=30)

        self.var_area2 = StringVar()
        self.Label_area_result2 = Label(self.frame, textvariable=self.var_area2, text="长方体的体积的结果", background="lightgray",foreground="blue", font=("微软雅黑", 12, "bold"))
        self.Label_area_result2.place(x=100, y=90)

        self.var_area3 = StringVar()
        self.Label_area_result3 = Label(self.frame, textvariable=self.var_area3, text="圆柱的体积的结果",background="lightgray", foreground="blue", font=("微软雅黑", 12, "bold"))
        self.Label_area_result3.place(x=100, y=150)

        self.var_area4 = StringVar()
        self.Label_area_result4 = Label(self.frame, textvariable=self.var_area4, text="圆锥的体积的结果",background="lightgray", foreground="blue", font=("微软雅黑", 12, "bold"))
        self.Label_area_result4.place(x=100, y=210)
    def check_input(self, radius: str):
        if radius=='':
            print("你输入为空")
        if radius.replace(".", "").isdigit():
            return True
        else:
            return False
    def get_area1(self, radius: float):
        PI = 3.1415926
        circle_area = PI * radius * radius
        return circle_area
    def get_area2(self, radius21: float,radius22: float,radius23: float):
        P=(radius21+radius22+radius23)/2
        circle_area2 = pow(P*(P-radius21)*(P-radius22)*(P-radius23),0.5)
        return circle_area2
    def get_area3(self, radius31: float, radius32: float):
        circle_area3 = radius31*radius32
        return circle_area3
    def get_area4(self, radius41: float, radius42: float, radius43: float):
        circle_area4 = (radius41+radius42)*radius43/2
        return circle_area4

    def get_vol1(self, radius: float):
        PI = 3.1415926
        circle_vol = 4*PI * radius * radius*radius/3
        return circle_vol
    def get_vol2(self, radius21: float,radius22: float,radius23: float):
        circle_vol2 = radius21*radius22*radius23
        return circle_vol2
    def get_vol3(self, radius31: float, radius32: float):
        PI = 3.1415926
        circle_vol3 = PI*radius31*radius31*radius32
        return circle_vol3
    def get_vol4(self, radius41: float, radius42: float):
        PI = 3.1415926
        circle_vol4 = PI*radius41*radius41*radius42/3
        return circle_vol4
    def get_result1(self):
        print("点击了体积计算按钮1")
        radius = self.var_radius.get()
        if self.check_input(radius):
            circle_area = self.get_vol1(float(radius))
            self.var_area.set("%.2f" % circle_area)
        else:
            showinfo("系统消息", "球的半径 不符合要求")
    def get_result2(self):
        print("点击了体积计算按钮2")
        radius21 = self.var_radius21.get()
        radius22 = self.var_radius22.get()
        radius23 = self.var_radius23.get()
        if self.check_input(radius21) and self.check_input(radius22) and self.check_input(radius23):
            circle_area2 = self.get_vol2(float(radius21),float(radius22),float(radius23))
            self.var_area2.set("%.2f" % circle_area2)
        else:
            showinfo("系统消息", "长方体三边 不符合要求")
    def get_result3(self):
        print("点击了体积计算按钮3")
        radius31 = self.var_radius31.get()
        radius32 = self.var_radius32.get()
        if self.check_input(radius31) and self.check_input(radius32):
            circle_area3 = self.get_vol3(float(radius31), float(radius32))
            self.var_area3.set("%.2f" % circle_area3)
        else:
            showinfo("系统消息", "圆柱 不符合要求")
    def get_result4(self):
        print("点击了体积计算按钮4")
        radius41 = self.var_radius41.get()
        radius42 = self.var_radius42.get()
        if self.check_input(radius41) and self.check_input(radius42):
            circle_area4 = self.get_vol4(float(radius41), float(radius42))
            self.var_area4.set("%.2f" % circle_area4)
        else:
            showinfo("系统消息", "圆锥 不符合要求")
    def show_a(self):
         self.frame.mainloop()

class Calculator3:
    def __init__(self):
        self.frame = tkinter.Toplevel(root)
        self.Button_call = Button(self.frame, text="计算二进制", command=self.get_result1)
        self.Button_call.place(x=500, y=0)
        self.frame.title("计算二进制")
        print("点击了二进制按钮")
        self.frame.geometry("600x180+700+300")
        self.frame.resizable(0, 0)
        self.frame["bg"] = "lightgray"

        # 添加一个Label 标签
        self.Label_input = Label(self.frame, text="请输入二进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_input.place(x=0, y=0)

        # 添加输入的文本框
        self.var_radius = StringVar()
        self.Entry_input = Entry(self.frame, textvariable=self.var_radius, font=("微软雅黑", 12, "bold"), width=30)
        self.Entry_input.place(x=130, y=0)

        self.Label_area2 = Label(self.frame, text="八进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area2.place(x=0, y=30)

        self.Label_area3 = Label(self.frame, text="十进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area3.place(x=0, y=90)

        self.Label_area4 = Label(self.frame, text="十六进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area4.place(x=0, y=150)

        self.var_area = StringVar()
        self.Label_area_result = Label(self.frame, textvariable=self.var_area, text="八进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area_result.place(x=100, y=30)

        self.var_area2 = StringVar()
        self.Label_area_result2 = Label(self.frame, textvariable=self.var_area2, text="十进制数", background="lightgray",foreground="blue", font=("微软雅黑", 12, "bold"))
        self.Label_area_result2.place(x=100, y=90)

        self.var_area3 = StringVar()
        self.Label_area_result3 = Label(self.frame, textvariable=self.var_area3, text="十六进制数",background="lightgray", foreground="blue", font=("微软雅黑", 12, "bold"))
        self.Label_area_result3.place(x=100, y=150)

    def check_input(self, radius: str):
        if radius=='':
            print("你输入为空")
        for i in radius:
            if i not in ['0','1']:
                return False
        return True
    def get_result1(self):
        print("点击二进制计算按钮")
        radius = self.var_radius.get()
        if self.check_input(radius):
            circle_area8 = oct(int(radius,base=2))
            self.var_area.set(circle_area8)
            circle_area10 = int(radius,base=2)
            self.var_area2.set(circle_area10)
            circle_area16 = hex(int(radius,base=2))
            self.var_area3.set(circle_area16)
        else:
            showinfo("系统消息", "输入二进制数 不符合要求")

    def show_a(self):
         self.frame.mainloop()

class Calculator4:
    def __init__(self):
        self.frame = tkinter.Toplevel(root)
        self.Button_call = Button(self.frame, text="计算八进制", command=self.get_result1)
        self.Button_call.place(x=500, y=0)
        self.frame.title("计算八进制")
        print("点击了八进制按钮")
        self.frame.geometry("600x180+700+400")
        self.frame.resizable(0, 0)
        self.frame["bg"] = "lightgray"

        # 添加一个Label 标签
        self.Label_input = Label(self.frame, text="请输入八进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_input.place(x=0, y=0)

        # 添加输入的文本框
        self.var_radius = StringVar()
        self.Entry_input = Entry(self.frame, textvariable=self.var_radius, font=("微软雅黑", 12, "bold"), width=30)
        self.Entry_input.place(x=130, y=0)

        self.Label_area2 = Label(self.frame, text="二进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area2.place(x=0, y=30)

        self.Label_area3 = Label(self.frame, text="十进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area3.place(x=0, y=90)

        self.Label_area4 = Label(self.frame, text="十六进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area4.place(x=0, y=150)

        self.var_area = StringVar()
        self.Label_area_result = Label(self.frame, textvariable=self.var_area, text="二进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area_result.place(x=100, y=30)

        self.var_area2 = StringVar()
        self.Label_area_result2 = Label(self.frame, textvariable=self.var_area2, text="十进制数", background="lightgray",foreground="blue", font=("微软雅黑", 12, "bold"))
        self.Label_area_result2.place(x=100, y=90)

        self.var_area3 = StringVar()
        self.Label_area_result3 = Label(self.frame, textvariable=self.var_area3, text="十六进制数",background="lightgray", foreground="blue", font=("微软雅黑", 12, "bold"))
        self.Label_area_result3.place(x=100, y=150)

    def check_input(self, radius: str):
        if radius=='':
            print("你输入为空")
        for i in radius:
            if i not in ['0','1','2','3','4','5','6','7']:
                return False
        return True
    def get_result1(self):
        print("点击八进制计算按钮")
        radius = self.var_radius.get()
        if self.check_input(radius):
            circle_area8 = bin(int(radius,base=8))
            self.var_area.set(circle_area8)
            circle_area10 = int(radius,base=8)
            self.var_area2.set(circle_area10)
            circle_area16 = hex(int(radius,base=8))
            self.var_area3.set(circle_area16)
        else:
            showinfo("系统消息", "输入八进制数 不符合要求")

    def show_a(self):
         self.frame.mainloop()

class Calculator5:
    def __init__(self):
        self.frame = tkinter.Toplevel(root)
        self.Button_call = Button(self.frame, text="计算十进制", command=self.get_result1)
        self.Button_call.place(x=500, y=0)
        self.frame.title("计算十进制")
        print("点击了十进制按钮")
        self.frame.geometry("600x180+700+500")
        self.frame.resizable(0, 0)
        self.frame["bg"] = "lightgray"

        # 添加一个Label 标签
        self.Label_input = Label(self.frame, text="请输入十进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_input.place(x=0, y=0)

        # 添加输入的文本框
        self.var_radius = StringVar()
        self.Entry_input = Entry(self.frame, textvariable=self.var_radius, font=("微软雅黑", 12, "bold"), width=30)
        self.Entry_input.place(x=130, y=0)

        self.Label_area2 = Label(self.frame, text="二进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area2.place(x=0, y=30)

        self.Label_area3 = Label(self.frame, text="八进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area3.place(x=0, y=90)

        self.Label_area4 = Label(self.frame, text="十六进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area4.place(x=0, y=150)

        self.var_area = StringVar()
        self.Label_area_result = Label(self.frame, textvariable=self.var_area, text="二进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area_result.place(x=100, y=30)

        self.var_area2 = StringVar()
        self.Label_area_result2 = Label(self.frame, textvariable=self.var_area2, text="八进制数", background="lightgray",foreground="blue", font=("微软雅黑", 12, "bold"))
        self.Label_area_result2.place(x=100, y=90)

        self.var_area3 = StringVar()
        self.Label_area_result3 = Label(self.frame, textvariable=self.var_area3, text="十六进制数",background="lightgray", foreground="blue", font=("微软雅黑", 12, "bold"))
        self.Label_area_result3.place(x=100, y=150)

    def check_input(self, radius: str):
        if radius=='':
            print("你输入为空")
        for i in radius:
            if i not in ['0','1','2','3','4','5','6','7','8','9']:
                return False
        return True
    def get_result1(self):
        print("点击十进制计算按钮")
        radius = self.var_radius.get()
        if self.check_input(radius):
            circle_area8 = bin(int(radius))
            self.var_area.set(circle_area8)
            circle_area10 = oct(int(radius))
            self.var_area2.set(circle_area10)
            circle_area16 = hex(int(radius))
            self.var_area3.set(circle_area16)
        else:
            showinfo("系统消息", "输入十进制数 不符合要求")

    def show_a(self):
         self.frame.mainloop()

class Calculator6:
    def __init__(self):
        self.frame = tkinter.Toplevel(root)
        self.Button_call = Button(self.frame, text="计算十六进制", command=self.get_result1)
        self.Button_call.place(x=500, y=0)
        self.frame.title("计算十六进制")
        print("点击了十六进制按钮")
        self.frame.geometry("600x180+700+600")
        self.frame.resizable(0, 0)
        self.frame["bg"] = "lightgray"

        # 添加一个Label 标签
        self.Label_input = Label(self.frame, text="请输入十六进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_input.place(x=0, y=0)

        # 添加输入的文本框
        self.var_radius = StringVar()
        self.Entry_input = Entry(self.frame, textvariable=self.var_radius, font=("微软雅黑", 12, "bold"), width=30)
        self.Entry_input.place(x=130, y=0)

        self.Label_area2 = Label(self.frame, text="二进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area2.place(x=0, y=30)

        self.Label_area3 = Label(self.frame, text="八进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area3.place(x=0, y=90)

        self.Label_area4 = Label(self.frame, text="十进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area4.place(x=0, y=150)

        self.var_area = StringVar()
        self.Label_area_result = Label(self.frame, textvariable=self.var_area, text="二进制数", background="lightgray", foreground="blue",font=("微软雅黑", 12, "bold"))
        self.Label_area_result.place(x=100, y=30)

        self.var_area2 = StringVar()
        self.Label_area_result2 = Label(self.frame, textvariable=self.var_area2, text="八进制数", background="lightgray",foreground="blue", font=("微软雅黑", 12, "bold"))
        self.Label_area_result2.place(x=100, y=90)

        self.var_area3 = StringVar()
        self.Label_area_result3 = Label(self.frame, textvariable=self.var_area3, text="十进制数",background="lightgray", foreground="blue", font=("微软雅黑", 12, "bold"))
        self.Label_area_result3.place(x=100, y=150)

    def check_input(self, radius: str):
        if radius=='':
            print("你输入为空")
        for i in radius:
            if i not in ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','A','B','C','D','E','F']:
                return False
        return True
    def get_result1(self):
        print("点击十六进制计算按钮")
        radius = self.var_radius.get()
        if self.check_input(radius):
            circle_area8 = bin(int(radius,base=16))
            self.var_area.set(circle_area8)
            circle_area10 = oct(int(radius,base=16))
            self.var_area2.set(circle_area10)
            circle_area16 = int(radius,base=16)
            self.var_area3.set(circle_area16)
        else:
            showinfo("系统消息", "输入十六进制数 不符合要求")

    def show_a(self):
         self.frame.mainloop()

if __name__ == '__main__':
    root=tkinter.Tk()
    # 设置窗口大小和位置
    root.geometry('300x330+400+100')
    #不允许改变窗口大小
    root.resizable(False,False)
    #设置窗口标题
    root.title('计算器')

    #字体设置
    EntryFont=tkfont.Font(root,size=13)
    ButtonFont=tkfont.Font(root,size=12)
    #放置用来显示信息的文本框,并设置为只读
    contentVar=tkinter.StringVar(root,'')
    # contentEntry=tkinter.Entry(root,textvariable=contentVar)
    # contentEntry['state']='readonly'
    # anchor的值可取 n, ne, e, se, s, sw, w, nw, or center
    contentEntry=tkinter.Label(root,bg='#EEE9E9',fg='black',anchor='w',font=EntryFont,textvariable=contentVar)
    contentEntry.place(x=10,y=10,width=280,height=20)

    #按钮通用代码
    def buttonClick(btn):
        content=contentVar.get()
        #如果已有内容是以小数点开头的,前面加0
        if content.startswith('.'):
            content='0'+ content
        #根据不同按钮做出相应的处理
        if btn in '0123456789':
            content += btn
        elif btn=='.':
        #使用运算符分隔表达式,最后一个运算数中最多只能有一个小数点
            lastPart=re.split(r'\+|-|\*|/]',content)[-1]
            if '.'in lastPart:
                tkinter.messagebox.showerror('错误','小数点太多了')
                return
            else:
                content += btn
        elif btn=='C':
            # 清空文本框中的表达式
            content =''
        elif btn == '=':
            try:
            # 对输入的表达式求值
                content = str(eval(content))
            except:
                tkinter.messagebox.showerror('错误','表达式错误')
                return
        elif btn=='back':
            content = content[:-1]
        elif btn in operators:
            if content.endswith(operators):
                tkinter.messagebox.showerror('错误','不允许存在连续运算符')
                return
            content += btn
        elif btn == 'Sqrt':
            n = content.split('.')
            if all(map(lambda x:x.isdigit(),n)):
                content = str(eval(content) ** 0.5)
            else :
                tkinter.messagebox.showerror('错误','表达式错误')
                return
        contentVar.set(content)

    # 放置清除按钮和等号按钮
    btnClear = tkinter.Button(root,text = 'Clear',relief=RIDGE,bg='#DC143C',font=ButtonFont,command=lambda:buttonClick('C'))
    btnClear.place(x=20,y=40,width=50,height=20)
    btnCompute=tkinter.Button(root,text='=',relief=FLAT,bg='#FFB6C1',command=lambda:buttonClick('='))
    btnCompute.place(x=90,y=40,width=50,height=20)
    btnComp=tkinter.Button(root,text='back',command=lambda:buttonClick('back'))
    btnComp.place(x=230,y=230,width=50,height=20)

    # btnarea= tkinter.Button(root,text = '面积',command=lambda:buttonClick('面积'))
    btnarea = tkinter.Button(root,text='面积',command=Calculator)
    btnarea.place(x=160,y=40,width=50,height=20)
    btnvol=tkinter.Button(root,text='体积',command=Calculator2)
    btnvol.place(x=230,y=40,width=50,height=20)

    btntwo = tkinter.Button(root,text='二进制',command=Calculator3)
    btntwo.place(x=20,y=280,width=50,height=20)
    btntwo = tkinter.Button(root, text='八进制', command=Calculator4)
    btntwo.place(x=90, y=280, width=50, height=20)
    btntwo = tkinter.Button(root, text='十进制', command=Calculator5)
    btntwo.place(x=160, y=280, width=50, height=20)
    btntwo = tkinter.Button(root, text='十六进制', command=Calculator6)
    btntwo.place(x=230, y=280, width=50, height=20)

    # 放置10个数字、小数点和计算平方根的按钮
    digits = list('0123456789.')+['Sqrt']
    index = 0
    for row in range(4):
        for col in range(3):
            d=digits[index]
            f=lambda x=d:buttonClick(x)
            index += 1
            btnDigit = tkinter.Button(root,text=d,command=f)
            btnDigit.place(x=20+col*70,y=80+row*50,width=50,height=20)

    # 放置运算符按钮
    operators=('+','-','*','/','**','//',"back")
    for index,operator in enumerate(operators):
        f=lambda x=operator:buttonClick(x)
        btnOperator=tkinter.Button(root,text=operator,relief=RAISED,bg='#FF1493',command=f)
        btnOperator.place(x=230,y=80+index*25,width=50,height=20)
    root.mainloop()
上一篇
下一篇