一个简单计算器(python)

import tkinter as tk
from tkinter import messagebox
class Calculator:
    def __init__(self, master):
        self.master = master
        master.title("简单计算器")
        # 创建输入框
        self.entry = tk.Entry(master, width=20, font=("Arial", 16))
        self.entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
        # 创建按钮
        self.create_button("7", 1, 0)
        self.create_button("8", 1, 1)
        self.create_button("9", 1, 2)
        self.create_button("+", 1, 3)
        self.create_button("4", 2, 0)
        self.create_button("5", 2, 1)
        self.create_button("6", 2, 2)
        self.create_button("-", 2, 3)
        self.create_button("1", 3, 0)
        self.create_button("2", 3, 1)
        self.create_button("3", 3, 2)
        self.create_button("*", 3, 3)
        self.create_button("0", 4, 0)
        self.create_button(".", 4, 1)
        self.create_button("=", 4, 2)
        self.create_button("/", 4, 3)

        # 创建清空按钮
        self.create_button("C", 5, 0, columnspan=2)
        self.create_button("DEL", 5, 2, columnspan=2)
    def create_button(self, text, row, column, columnspan=1):
        button = tk.Button(self.master, text=text, width=5, height=2, font=("Arial", 16), command=lambda: self.on_button_click(text))
        button.grid(row=row, column=column, columnspan=columnspan, padx=5, pady=5)
    def on_button_click(self, text):
        if text == "=":
            try:
                result = eval(self.entry.get())
                self.entry.delete(0, tk.END)
                self.entry.insert(tk.END, str(result))
            except:
                messagebox.showerror("错误", "输入有误")
        elif text == "C":
            self.entry.delete(0, tk.END)
        elif text == "DEL":
            self.entry.delete(len(self.entry.get())-1, tk.END)
        else:
            self.entry.insert(tk.END, text)
root = tk.Tk()
calculator = Calculator(root)
root.mainloop()

一个简单计算器(python)》有9个想法

  1. anak haram tetangga

    It is the best time to make some plans for the future and it is time to be happy.
    I’ve read this post and if I could I want to suggest
    you some interesting things or suggestions.
    Perhaps you can write next articles referring to this article.

    I desire to read even more things about it!

    回复
  2. anak tetangga haram

    Does your blog have a contact page? I’m having trouble locating
    it but, I’d like to shoot you an e-mail. I’ve got some ideas for your blog you might be interested in hearing.
    Either way, great site and I look forward to seeing it expand over time.

    回复
  3. anak haram tetangga

    Hello there, I do think your web site might be having browser compatibility problems.
    Whenever I look at your blog in Safari, it looks fine however, if opening in Internet Explorer, it’s got some overlapping issues.
    I simply wanted to provide you with a quick heads up! Apart
    from that, fantastic blog!

    回复
  4. https://tiptop108.info/

    I’m extremely impressed along with your writing abilities as neatly
    as with the layout for your blog. Is that this a paid subject or
    did you customize it your self? Anyway stay up the nice quality writing, it’s
    uncommon to see a nice weblog like this one today..

    回复
  5. Kometa welcome bonus

    Discover the world of excitement at Kometa Casino —
    a platform for true game lovers, where you will find
    the best slots, classic table games, and exclusive offers to help increase your winnings!
    Casino bonuses.

    Why choose Kometa Casino?

    Instant payouts without hidden charges.

    A variety of entertainment for every taste.

    Exclusive offers, making the game even more profitable.

    Start playing right now and feel the real excitement! https://kometa-777-spin.beauty/

    回复

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注