python使用tkinter实现简单计算器

yizhihongxing

下面是详细讲解“python使用tkinter实现简单计算器”的完整攻略。

第一步:导入tkinter库

在开始编写计算器之前,需要先导入Tkinter库。可以使用以下代码导入:

from tkinter import *

第二步:创建主窗口和添加组件

在导入库之后,需要创建主窗口并为其添加所需组件。这个过程可以使用以下代码完成:

root = Tk()
root.title("计算器")

# 创建一个输入框
entry = Entry(root, width=35, borderwidth=5)
entry.grid(row=0, column=0, columnspan=3, padx=10, pady=10)

# 创建按钮
button_1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_2 = Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_3 = Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_4 = Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_5 = Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_6 = Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_7 = Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_8 = Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_9 = Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_0 = Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0))

button_add = Button(root, text="+", padx=39, pady=20, command=button_add)
button_subtract = Button(root, text="-", padx=41, pady=20, command=button_subtract)
button_multiply = Button(root, text="*", padx=40, pady=20, command=button_multiply)
button_divide = Button(root, text="/", padx=41, pady=20, command=button_divide)

button_clear = Button(root, text="清除", padx=79, pady=20, command=button_clear)
button_equal = Button(root, text="=", padx=91, pady=20, command=button_equal)

在这段代码中,我们创建了一个主窗口,并为其添加了一个输入框和所有需要用到的按钮。

第三步:定义按钮点击事件

下一步是为每个按钮定义单击事件。这可以使用以下代码完成:

# 按钮单击事件
def button_click(number):
    current = entry.get()
    entry.delete(0, END)
    entry.insert(0, str(current) + str(number))

def button_add():
    first_number = entry.get()
    global f_num
    global math
    math = "addition"
    f_num = int(first_number)
    entry.delete(0, END)

def button_subtract():
    first_number = entry.get()
    global f_num
    global math
    math = "subtraction"
    f_num = int(first_number)
    entry.delete(0, END)

def button_multiply():
    first_number = entry.get()
    global f_num
    global math
    math = "multiplication"
    f_num = int(first_number)
    entry.delete(0, END)

def button_divide():
    first_number = entry.get()
    global f_num
    global math
    math = "division"
    f_num = int(first_number)
    entry.delete(0, END)

def button_clear():
    entry.delete(0, END)

def button_equal():
    second_number = entry.get()
    entry.delete(0, END)

    if math == "addition":
        entry.insert(0, f_num + int(second_number))
    elif math == "subtraction":
        entry.insert(0, f_num - int(second_number))
    elif math == "multiplication":
        entry.insert(0, f_num * int(second_number))
    elif math == "division":
        entry.insert(0, f_num / int(second_number))

在这段代码中,我们定义了单击事件(也称为回调函数)来处理按钮的点击。这些回调函数将执行根据所点击的按钮执行的相应操作。

第四步:运行计算器

定义完回调函数之后,可以通过以下代码运行计算器:

# 创建按钮的布局
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)

button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)

button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)

button_0.grid(row=4, column=0)

button_clear.grid(row=4, column=1, columnspan=2)
button_add.grid(row=5, column=0)
button_equal.grid(row=5, column=1, columnspan=2)

button_subtract.grid(row=6, column=0)
button_multiply.grid(row=6, column=1)
button_divide.grid(row=6, column=2)

root.mainloop()

这行代码运行计算器并将其显示在屏幕上。

示例1

下面是示例1,它将演示如何使用Python Tkinter实现一个简单的计算器。

from tkinter import *
root = Tk()
root.title("计算器")

# 创建一个输入框
entry = Entry(root, width=35, borderwidth=5)
entry.grid(row=0, column=0, columnspan=3, padx=10, pady=10)

# 创建按钮
button_1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_2 = Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_3 = Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_4 = Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_5 = Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_6 = Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_7 = Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_8 = Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_9 = Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_0 = Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0))

button_add = Button(root, text="+", padx=39, pady=20, command=button_add)
button_subtract = Button(root, text="-", padx=41, pady=20, command=button_subtract)
button_multiply = Button(root, text="*", padx=40, pady=20, command=button_multiply)
button_divide = Button(root, text="/", padx=41, pady=20, command=button_divide)

button_clear = Button(root, text="清除", padx=79, pady=20, command=button_clear)
button_equal = Button(root, text="=", padx=91, pady=20, command=button_equal)

# 按钮单击事件
def button_click(number):
    current = entry.get()
    entry.delete(0, END)
    entry.insert(0, str(current) + str(number))

def button_add():
    first_number = entry.get()
    global f_num
    global math
    math = "addition"
    f_num = int(first_number)
    entry.delete(0, END)

def button_subtract():
    first_number = entry.get()
    global f_num
    global math
    math = "subtraction"
    f_num = int(first_number)
    entry.delete(0, END)

def button_multiply():
    first_number = entry.get()
    global f_num
    global math
    math = "multiplication"
    f_num = int(first_number)
    entry.delete(0, END)

def button_divide():
    first_number = entry.get()
    global f_num
    global math
    math = "division"
    f_num = int(first_number)
    entry.delete(0, END)

def button_clear():
    entry.delete(0, END)

def button_equal():
    second_number = entry.get()
    entry.delete(0, END)

    if math == "addition":
        entry.insert(0, f_num + int(second_number))
    elif math == "subtraction":
        entry.insert(0, f_num - int(second_number))
    elif math == "multiplication":
        entry.insert(0, f_num * int(second_number))
    elif math == "division":
        entry.insert(0, f_num / int(second_number))

# 创建按钮的布局
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)

button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)

button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)

button_0.grid(row=4, column=0)

button_clear.grid(row=4, column=1, columnspan=2)
button_add.grid(row=5, column=0)
button_equal.grid(row=5, column=1, columnspan=2)

button_subtract.grid(row=6, column=0)
button_multiply.grid(row=6, column=1)
button_divide.grid(row=6, column=2)

root.mainloop()

当你运行这个程序时,会看到计算器的界面。

示例2

下面是示例2,它演示了如何将计算器与其他代码集成起来。例如,可以修改按钮中的回调函数来对计算器执行其他操作,而不仅仅是简单的数学计算。

from tkinter import *

# 按钮单击事件
def button_click(number):
    current = entry.get()
    entry.delete(0, END)
    entry.insert(0, str(current) + str(number))

def button_add():
    first_number = entry.get()
    global f_num
    global math
    math = "addition"
    f_num = int(first_number)
    entry.delete(0, END)

def button_subtract():
    first_number = entry.get()
    global f_num
    global math
    math = "subtraction"
    f_num = int(first_number)
    entry.delete(0, END)

def button_multiply():
    first_number = entry.get()
    global f_num
    global math
    math = "multiplication"
    f_num = int(first_number)
    entry.delete(0, END)

def button_divide():
    first_number = entry.get()
    global f_num
    global math
    math = "division"
    f_num = int(first_number)
    entry.delete(0, END)

def button_clear():
    entry.delete(0, END)

def button_equal():
    second_number = entry.get()
    entry.delete(0, END)

    if math == "addition":
        entry.insert(0, f_num + int(second_number))
    elif math == "subtraction":
        entry.insert(0, f_num - int(second_number))
    elif math == "multiplication":
        entry.insert(0, f_num * int(second_number))
    elif math == "division":
        entry.insert(0, f_num / int(second_number))

# 创建一个主窗口
root = Tk()
root.title("计算器")

# 创建一个输入框
entry = Entry(root, width=35, borderwidth=5)
entry.grid(row=0, column=0, columnspan=3, padx=10, pady=10)

# 创建按钮
button_1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_2 = Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_3 = Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_4 = Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_5 = Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_6 = Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_7 = Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_8 = Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_9 = Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_0 = Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0))

button_add = Button(root, text="+", padx=39, pady=20, command=button_add)
button_subtract = Button(root, text="-", padx=41, pady=20, command=button_subtract)
button_multiply = Button(root, text="*", padx=40, pady=20, command=button_multiply)
button_divide = Button(root, text="/", padx=41, pady=20, command=button_divide)

button_clear = Button(root, text="清除", padx=79, pady=20, command=button_clear)
button_equal = Button(root, text="=", padx=91, pady=20, command=button_equal)

# 创建按钮的布局
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)

button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)

button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)

button_0.grid(row=4, column=0)

button_clear.grid(row=4, column=1, columnspan=2)
button_add.grid(row=5, column=0)
button_equal.grid(row=5, column=1, columnspan=2)

button_subtract.grid(row=6, column=0)
button_multiply.grid(row=6, column=1)
button_divide.grid(row=6, column=2)

# 在计算器外部将计算结果导入到另一个便签
result_label = Label(root, text="计算结果:")
result_label.grid(row=7, column=0)

result_text = Label(root, text="")
result_text.grid(row=7, column=1)

# 隐藏计算结果同步到另一个标签的功能
def button_equal():
    second_number = entry.get()
    entry.delete(0, END)

    if math == "addition":
        result = f_num + int(second_number)
        entry.insert(0, result)
        result_text.config(text=result)
    elif math == "subtraction":
        result = f_num - int(second_number)
        entry.insert(0, result)
        result_text.config(text=result)
    elif math == "multiplication":
        result = f_num * int(second_number)
        entry.insert(0, result)
        result_text.config(text=result)
    elif math == "division":
        result = f_num / int(second_number)
        entry.insert(0, result)
        result_text.config(text=result)

# 运行主循环
root.mainloop()

在这个示例中,我们添加了一个标签,用于在计算结果被计算时显示结果的值。我们还添加了一行代码,将计算结果同步到另一个标签(result_text)。现在,每当单击“=”按钮时,计算结果都将显示在entry框中和另一个标签中。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python使用tkinter实现简单计算器 - Python技术站

(0)
上一篇 2023年6月13日
下一篇 2023年6月13日

相关文章

  • python的id()函数介绍

    Python的id()函数介绍 简介 在Python中,每个对象都有一个地址,id()函数返回该对象的内存地址。其实,这个地址就是该对象在内存中的位置。 用法 id()函数的调用方式很简单,只需要在函数中传入要获取地址的对象即可返回该对象的地址。 >>> a = 10 >>> id(a) 10914784 示例 示例1 &…

    python 2023年6月5日
    00
  • 树莓派 Crontab Python 脚本需要写权限

    【问题标题】:Raspberry Pi Crontab Python script needs write permission树莓派 Crontab Python 脚本需要写权限 【发布时间】:2023-04-04 02:34:01 【问题描述】: 我有一个 python 脚本,我想每 15 分钟在我的树莓派上运行一次。该脚本应该做一些事情,然后将报告发布…

    Python开发 2023年4月6日
    00
  • 学习python的几条建议分享

    下面是详细讲解“学习Python的几条建议分享”的攻略: 学习Python的几条建议分享 初学入门建议 选择合适的教材和学习路径:由于Python学习资料较多,建议选择一本经典入门教材(例如谢希仁的《Python 语言程序设计》),并按照系统化的章节顺序进行学习,练习每一章节的例子,保证理解后再进入下一章节。 注重实践:Python是一种实用性语言,学习要注…

    python 2023年5月18日
    00
  • Python 语法错误:”SyntaxError: invalid character in identifier”原因与解决办法

    当我们在Python编程中定义变量或函数时,如果变量或函数名中包含非法字符,就会出现“SyntaxError: invalid character in identifier”的语法错误。以下是解决Python语法错误“SyntaxError: invalid character in identifier”的完整攻略。 原因 Python中的标识符(变量名…

    python 2023年5月13日
    00
  • 基于Python实现商场抽奖小系统

    下面是基于Python实现商场抽奖小系统的完整攻略: 1. 确定系统需求 在开始编写代码前,我们需要先明确这个抽奖小系统需要具备哪些功能,例如: 能够生成一定数量的奖品,并将奖品存储在数据库中 能够在数据库中添加、删除、修改奖品的信息 能够在抽奖时从数据库中获取奖品信息,并展示给用户 能够实现抽奖过程,并在最终抽中奖品后将相关信息存储在数据库中 能够展示抽奖…

    python 2023年6月13日
    00
  • Python tkinter库绘图实例分享

    下面我将详细讲解“Python tkinter库绘图实例分享”的完整攻略。 Python tkinter库绘图实例分享 1. 简介 Python提供了多个图形库,其中Tkinter是Python自带的图形库之一,是Python的标准GUI库。Tkinter提供了快速、简单的方法来构建基于GUI的应用程序。在Tkinter中,可以通过Canvas(画布)类来创…

    python 2023年5月19日
    00
  • python字符串过滤性能比较5种方法

    以下是详细讲解“Python字符串过滤性能比较5种方法”的完整攻略。 1. 问题描述 在Python中,我们经常需要字符串进行过滤,以提取或删除特定的字符或串。本文将介绍Python字符串过滤的5种方法,并比较它们的性能。 2. 解决方法 在Python中,我们可以使用5种方法对字符串进行过滤,分别是: 方法1:使用for循环和if语句 def filter…

    python 2023年5月14日
    00
  • 创建奇数索引之和python

    【问题标题】:creating sum of odd indexes python创建奇数索引之和python 【发布时间】:2023-04-02 22:30:01 【问题描述】: 我正在尝试创建一个等于列表中所有其他数字之和的函数。例如,如果列表为 [0,1,2,3,4,5],则函数应等于 5+3+1。我怎么能这样做?我对 Python 的了解并没有比 w…

    Python开发 2023年4月8日
    00
合作推广
合作推广
分享本页
返回顶部