猫史档案馆


tkinter版计算器,数字过大竟然会卡喵。。。

用户:科学边界科学边界查看:0 回复:0 评论:0 创建时间:2024-07-22T14:21:11


有问题欢迎评论哦

源码:

import tkinter as tk
import tkinter.messagebox as tkmessage
class JiSuanQi:
    def __init__(self):
        self.w = tk.Tk()
        self.w.title('计算器')
        self.w.geometry('200x300')
        self.w.resizable(False, False)
        self.inputText = ''
        self.mainBoard = ['(', ')', '^', '+',
                          '7', '8', '9', '-',
                          '4', '5', '6', '×',
                          '1', '2', '3', '÷',
                          'AC', '0', '.', '=']

    def main(self) -> None:
        self.entry = tk.Entry(self.w, font=('微软雅黑', 15), state='readonly', width=15)
        self.entry.place(x=10, y=10)
        tk.Button(self.w, text=self.mainBoard[0],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[0]),
                  width=3).place(x=0, y=50)
        tk.Button(self.w, text=self.mainBoard[1],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[1]),
                  width=3).place(x=50, y=50)
        tk.Button(self.w, text=self.mainBoard[2],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[2]),
                  width=3).place(x=100, y=50)
        tk.Button(self.w, text=self.mainBoard[3],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[3]),
                  width=3).place(x=150, y=50)
        tk.Button(self.w, text=self.mainBoard[4],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[4]),
                  width=3).place(x=0, y=100)
        tk.Button(self.w, text=self.mainBoard[5],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[5]),
                  width=3).place(x=50, y=100)
        tk.Button(self.w, text=self.mainBoard[6],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[6]),
                  width=3).place(x=100, y=100)
        tk.Button(self.w, text=self.mainBoard[7],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[7]),
                  width=3).place(x=150, y=100)
        tk.Button(self.w, text=self.mainBoard[8],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[8]),
                  width=3).place(x=0, y=150)
        tk.Button(self.w, text=self.mainBoard[9],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[9]),
                  width=3).place(x=50, y=150)
        tk.Button(self.w, text=self.mainBoard[10],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[10]),
                  width=3).place(x=100, y=150)
        tk.Button(self.w, text=self.mainBoard[11],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[11]),
                  width=3).place(x=150, y=150)
        tk.Button(self.w, text=self.mainBoard[12],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[12]),
                  width=3).place(x=0, y=200)
        tk.Button(self.w, text=self.mainBoard[13],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[13]),
                  width=3).place(x=50, y=200)
        tk.Button(self.w, text=self.mainBoard[14],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[14]),
                  width=3).place(x=100, y=200)
        tk.Button(self.w, text=self.mainBoard[15],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[15]),
                  width=3).place(x=150, y=200)
        tk.Button(self.w, text=self.mainBoard[16],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[16]),
                  width=3).place(x=0, y=250)
        tk.Button(self.w, text=self.mainBoard[17],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[17]),
                  width=3).place(x=50, y=250)
        tk.Button(self.w, text=self.mainBoard[18],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[18]),
                  width=3).place(x=100, y=250)
        tk.Button(self.w, text=self.mainBoard[19],
                  font=('微软雅黑', 15),
                  command=lambda: self.inputEntry(self.mainBoard[19]),
                  width=3).place(x=150, y=250)
        self.w.mainloop()
    def inputEntry(self, text: str) -> None:
        if text == 'AC':
            self.inputText = ''
            self.entry.config(state='normal')
            self.entry.delete(0, tk.END)
            self.entry.config(state='readonly')
            return
        elif text == '=':
            self.JiSuan()
            return
        elif text == '^':
            self.inputText += '**'
        elif text == '×':
            self.inputText += '*'
        elif text == '÷':
            self.inputText += '/'
        else:
            self.inputText += text
        self.entry.config(state='normal')
        self.entry.insert(tk.END, text)
        self.entry.config(state='readonly')
    def JiSuan(self) -> None:
        try:
            tkmessage.showinfo('计算结果', str(eval(self.inputText)))
        except:
            tkmessage.showerror('error', 'error')
        self.entry.config(state='normal')
        self.entry.delete(0, tk.END)
        self.entry.config(state='readonly')
        self.inputText = ''
if __name__ == '__main__':
    a = JiSuanQi()
    a.main()


回复

上一页1 页 / 共 0下一页