python tkinter 做个简单的计算器的方法

yizhihongxing

下面是关于Python Tkinter做计算器的完整攻略。

确认开发环境

要在Python中开发GUI应用程序,必须使用tkinter模块。大多数Python发行版都已经包含了Tk和tkinter,也可以使用pip来安装。确保安装了Python Tkinter的最新版本。

导入tkinter模块

在Python中,要使用tkinter,首先需要导入库:

import tkinter as tk

创建GUI应用程序

创建GUI应用程序时,需要创建一个主窗口。在tkinter中,可以使用Tk()来实现:

# 创建主窗口
root = tk.Tk()  
root.title("Calculator")

添加显示屏幕

计算器需要一个显示屏幕来显示结果,可以使用tkinter中的Entry组件来实现。在这里创建Entry组件,设置为只读:

# 添加显示屏幕
e = tk.Entry(root, width=35, borderwidth=5,font=('Helvetica', 10, 'bold'),justify='right',state='readonly')
e.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

我们这里定义了显示屏幕(即Entry)的一些属性,包括宽度、边框宽度、字体、对齐方式等。

定义数值按钮和普通按钮

计算器有10个数字按钮,加减乘除符号以及小数点按钮。我们可以写一个函数来处理所有这些数值:

# 定义数值按钮和普通按钮
def button_click(number):
    current = e.get()
    e.configure(state='normal')
    e.delete(0, tk.END)
    e.insert(0, str(current) + str(number))
    e.configure(state='readonly')

button_1 = tk.Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_1.grid(row=1, column=0)

button_2 = tk.Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_2.grid(row=1, column=1)

# 诸如此类,剩下的我就不一一列举了,可以自己补充

定义算法按钮

计算器还需要加减乘除符号等算法按钮。单击这些按钮后,它们应向显示器添加相应的符号,并准备进行计算。

# 定义加减乘除按钮
def button_add():
    first_number = e.get()
    global f_num
    global math
    math = "addition"
    f_num = float(first_number)
    e.delete(0, tk.END)

def button_subtract():
    first_number = e.get()
    global f_num
    global math
    math = "subtraction"
    f_num = float(first_number)
    e.delete(0, tk.END)

def button_multiply():
    first_number = e.get()
    global f_num
    global math
    math = "multiplication"
    f_num = float(first_number)
    e.delete(0, tk.END)

def button_divide():
    first_number = e.get()
    global f_num
    global math
    math = "division"
    f_num = float(first_number)
    e.delete(0, tk.END)

def button_clear():
    e.delete(0, tk.END)

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

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

将所有按钮添加到图形界面

最后,将所有按钮添加到主窗口中。

# 将所有按钮添加到图形界面
button_1 = tk.Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_1.grid(row=1, column=0)

button_2 = tk.Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_2.grid(row=1, column=1)

# 诸如此类,剩下的我就不一一列举了,可以自己补充

# 数学操作按钮
button_add = tk.Button(root, text="+", padx=39, pady=20, command=button_add)
button_add.grid(row=5, column=0)

button_subtract = tk.Button(root, text="-", padx=41, pady=20, command=button_subtract)
button_subtract.grid(row=4, column=0)

button_multiply = tk.Button(root, text="*", padx=40, pady=20, command=button_multiply)
button_multiply.grid(row=3, column=0)

button_divide = tk.Button(root, text="/", padx=41, pady=20, command=button_divide)
button_divide.grid(row=2, column=0)

button_clear = tk.Button(root, text="Clear", padx=79, pady=20, command=button_clear)
button_clear.grid(row=6, column=1, columnspan=2)

button_equal = tk.Button(root, text="=", padx=91, pady=20, command=button_equal)
button_equal.grid(row=6, column=3, columnspan=2)

示例

下面是一个完整的计算器程序示例:

import tkinter as tk

def button_click(number):
    current = e.get()
    e.configure(state='normal')
    e.delete(0, tk.END)
    e.insert(0, str(current) + str(number))
    e.configure(state='readonly')

def button_add():
    first_number = e.get()
    global f_num
    global math
    math = "addition"
    f_num = float(first_number)
    e.delete(0, tk.END)

def button_subtract():
    first_number = e.get()
    global f_num
    global math
    math = "subtraction"
    f_num = float(first_number)
    e.delete(0, tk.END)

def button_multiply():
    first_number = e.get()
    global f_num
    global math
    math = "multiplication"
    f_num = float(first_number)
    e.delete(0, tk.END)

def button_divide():
    first_number = e.get()
    global f_num
    global math
    math = "division"
    f_num = float(first_number)
    e.delete(0, tk.END)

def button_clear():
    e.delete(0, tk.END)

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

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

# 创建主窗口
root = tk.Tk()
root.title("Calculator")

# 添加显示屏幕
e = tk.Entry(root, width=35, borderwidth=5,font=('Helvetica', 10, 'bold'),justify='right',state='readonly')
e.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

# 数值按钮和普通按钮
button_1 = tk.Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_1.grid(row=1, column=0)

button_2 = tk.Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_2.grid(row=1, column=1)

button_3 = tk.Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_3.grid(row=1, column=2)

button_4 = tk.Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_4.grid(row=2, column=0)

button_5 = tk.Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_5.grid(row=2, column=1)

button_6 = tk.Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_6.grid(row=2, column=2)

button_7 = tk.Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_7.grid(row=3, column=0)

button_8 = tk.Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_8.grid(row=3, column=1)

button_9 = tk.Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_9.grid(row=3, column=2)

button_0 = tk.Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0))
button_0.grid(row=4, column=0)

button_dot = tk.Button(root, text=".", padx=42, pady=20, command=lambda: button_click("."))
button_dot.grid(row=4, column=1)

# 数学操作按钮
button_add = tk.Button(root, text="+", padx=39, pady=20, command=button_add)
button_add.grid(row=5, column=0)

button_subtract = tk.Button(root, text="-", padx=41, pady=20, command=button_subtract)
button_subtract.grid(row=4, column=0)

button_multiply = tk.Button(root, text="*", padx=40, pady=20, command=button_multiply)
button_multiply.grid(row=3, column=0)

button_divide = tk.Button(root, text="/", padx=41, pady=20, command=button_divide)
button_divide.grid(row=2, column=0)

# 其他按钮
button_clear = tk.Button(root, text="Clear", padx=79, pady=20, command=button_clear)
button_clear.grid(row=6, column=1, columnspan=2)

button_equal = tk.Button(root, text="=", padx=91, pady=20, command=button_equal)
button_equal.grid(row=6, column=3, columnspan=2)

root.mainloop()

此计算器程序可以将所有数字、小数点和算法组合在一起以进行计算,而且界面布局也相当简洁梳理。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python tkinter 做个简单的计算器的方法 - Python技术站

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

相关文章

  • Python字符编码转码之GBK,UTF8互转

    Python字符编码转码之GBK,UTF8互转 在处理文本数据时,我们常常需要进行字符编码转码的操作。本篇文章将介绍如何使用Python实现GBK和UTF8之间的互相转换。 编码 “encode” 和解码 “decode” 在Python中,字符串的编码 “encode” 和解码 “decode” 分别使用 encode() 和 decode() 方法实现。…

    python 2023年5月31日
    00
  • 深入理解Python 代码优化详解

    深入理解Python 代码优化详解 代码优化是提高Python程序性能的关键。本文将分享一些实用的技巧,以帮助开发人员优化他们的Python代码。 1. 使用Python内置函数代替循环 Python中内置了许多高效的函数,可以代替常规的循环,从而提高程序的性能。以下是一些示例: sum():用于计算一个列表中所有元素的和。可以避免使用for循环遍历整个列表…

    python 2023年6月5日
    00
  • python定时器使用示例分享

    Python定时器是一个非常实用的工具,它可以在规定的时间内执行特定的操作。在这篇文章中,我们将分享两个定时器的使用示例,帮助您了解如何使用Python定时器来自动执行任务。下面是操作步骤: 1. 安装定时器模块 Python自带了定时器模块time,可以使用该模块创建定时器并执行定时任务。 2. 示例1:使用time模块中的sleep函数实现定时器 使用t…

    python 2023年6月2日
    00
  • 详解小白之KMP算法及python实现

    详解小白之KMP算法及Python实现 KMP算法是一种字符串匹配算法,它可以在O(n+m)的时间复杂度内解决字符串匹配问题。本文将详细讲解KMP算法的原理、实现过程和代码实现,并提供两个示例说明。 算法原理 KMP算法的基本思想是利用已知信息,尽可能减少匹配的次数。具体实现过程如下: 一个next数组,用于存储模式串中每个字符前面的最长公共前后缀长度。 遍…

    python 2023年5月14日
    00
  • python实现学生信息管理系统(精简版)

    Python实现学生信息管理系统(精简版)攻略 简介 本文将介绍如何使用Python编写一个简单的学生信息管理系统。在该系统中,我们将使用文件来存储学生信息,并提供添加、删除、修改、查询等基本功能。在操作时,我们将使用命令行交互,用户可以根据提示输入相应的指令来实现操作。 环境配置 在开始之前,我们需要确保Python环境已经安装并正确配置。 首先,我们需要…

    python 2023年5月19日
    00
  • Python函数中不定长参数的写法

    Python中有时会有需要传入不定数量的参数给函数,这时就需要使用不定长参数。本文将详细讲解Python函数中不定长参数的写法,包括位置参数、关键字参数、强制关键字参数等。并且,我们将提供两个示例来帮助您更好地理解这个概念。 位置参数 位置参数其实就是Python中最基本的、最常用的参数类型。它指的是在函数定义中指定的参数,也就是通过位置来匹配实参的方式进行…

    python 2023年5月14日
    00
  • Python 获取指定开头指定结尾所夹中间内容(推荐)

    在Python中,我们可以使用正则表达式或字符串方法来获取指定开头和结尾之间的内容。以下是一些示例代码,演示如何使用正则表达式和字符串方法来获取指定开头和结尾之间的内容。 示例1:使用正则表达式获取指定开头和结尾之间的内容 以下是一个示例代码,用于使用正则表达式获取指定开头和结尾之间的内容: import re text = ‘The quick brown…

    python 2023年5月15日
    00
  • Python利用itchat模块定时给朋友发送微信信息

    Python利用itchat模块定时给朋友发送微信信息是一个非常有用的应用场景,可以帮助我们在Python中快速实现微信自动发送消息的功能。本攻略将介绍Python利用itchat模块定时给朋友发送微信信息的完整攻略,包括itchat模块的安装、微信登录、定时任务的实现和示例。 步骤1:安装itchat模块 在Python中,我们可以使用pip命令安装itc…

    python 2023年5月15日
    00
合作推广
合作推广
分享本页
返回顶部