在Python中使用 Tkinter 评估数学表达式
本文重点介绍使用Python中的Tkinter和math包评估数学表达式。
Tkinter: Python Tkinter 是一个 GUI 编程包或内置包。 Tkinter 为 Tk GUI 工具包提供了强大的面向对象接口。带有 Tkinter 的Python是创建 GUI 应用程序的最快和最简单的方法。使用 Tkinter 创建 GUI 是一项简单的任务。
数学模块:在Python中,可以通过导入一个名为“math”的Python模块来轻松进行各种数学运算,该模块指定了各种功能,使我们的任务更简单。
Steps involved in conversion of temperature:
- Importing the tkinter & math packages.
- Create the main window.
- Add number of widgets to the main window : Entry , Label.
- Evaluating the expression.
- Displaying message.
- Apply the event trigger on the widgets.
PYTHON
# Importing tkinter module as tk
import tkinter as tk
# Importing all functions/methods
# from math module
from math import *
# Import messagebox class from tkinter
from tkinter import messagebox
# function for evaluating the expression
def eval_expression(event):
result.configure(text = " Result: " +
str(eval(entry.get())))
messagebox.showinfo("Evaluate Expression",
"Successfully evaluated" )
# creating Tk window
root = tk.Tk()
# set geometry of root window
root.geometry('300x150+600+200')
# set the title of root window
root.title('Evaluate Expression')
# label and entry field
input_label = tk.Label(root,
text = " Enter Your Expression:",).grid(row = 1)
entry = tk.Entry(root)
# bind 'enter' event to the
# eval_expression() through
# entry widget
entry.bind("
输出 :