使用 Python 中的 Tkinter 制作简单的 GUI 计算器

pythonserver side programmingprogramming

在本教程中,我们将使用 Tkinter  模块创建一个简单的 GUI 计算器。Tkinter  是用于开发 GUI 应用程序的内置 Python 模块。它易于使用,并随 Python 一起提供。我们可以使用 GUI 应用程序可视化我们的数据。

让我们看看如何创建一个简单的 GUI 计算器。

  • 使用 * 从 Tkinter  导入所有内容。

  • 为计算器创建一个界面。

  • 创建一个输入函数,将数字输入到输入字段中。

  • 创建一个明显的函数,从输入字段中清除所有内容。

  • 最后,评估计算并给出表达式结果的函数。

示例

# 从 tkinter 导入所有内容
从 tkinter 导入 *
# 在所有函数之间访问的表达式
expression = ""
# 函数
def input_number(number, equation):
   # 访问全局表达式变量
   global expression
   # 字符串连接
   expression = expression + str(number)
   equation.set(expression)
def clear_input_field(equation):
   global expression
   expression = ""
   # 在输入字段中设置空字符串
   equation.set("输入表达式")
def assess(equation):
global expression
# 尝试求值表达式
try:
result = str(eval(expression))
# 在输入字段中显示结果
equation.set(result)
# 将表达式设置为空字符串
expression = ""
except:
# 发生错误
# 向用户显示 equation.set("输入有效表达式")
expression = ""
# 创建 GUI
def main():
   # main window window = Tk()
   # setting the title of GUI window
   window.title("Calculator")
   # set the configuration of GUI window
   window.geometry("325x175")
   # varible class instantiation
   equation = StringVar()
   # input field for the expression
   input_field = Entry(window, textvariable=equation)
   input_field.place(height=100)
   # we are using grid position
   # for the arrangement of the widgets
   input_field.grid(columnspan=4, ipadx=100, ipady=5)
   # settin the placeholder message for users
   equation.set("Enter the expression")
   # creating buttons and placing them at respective positions
   _1 = Button(window, text='1', fg='white', bg='black', bd=0, command=lambda: input_number(1, equation), height=2, width=7)
   _1.grid(row=2, column=0)
   _2 = Button(window, text='2', fg='white', bg='black', bd=0, command=lambda: input_number(2, equation), height=2, width=7)
   _2.grid(row=2, column=1)
   _3 = Button(window, text='3', fg='white', bg='black', bd=0, command=lambda: input_number(3, equation), height=2, width=7)
   _3.grid(row=2, column=2)
   _4 = Button(window, text='4', fg='white', bg='black', bd=0, command=lambda: input_number(4, equation), height=2, width=7)
   _4.grid(row=3, column=0)
   _5 = Button(window, text='5', fg='white', bg='black', bd=0, command=lambda: input_number(5, equation), height=2, width=7)
   _5.grid(row=3, column=1)
   _6 = Button(window, text='6', fg='white', bg='black', bd=0, command=lambda: input_number(6, equation), height=2, width=7)
   _6.grid(row=3, column=2)
   _7 = Button(window, text='7', fg='white', bg='black', bd=0, command=lambda: input_number(7, equation), height=2, width=7)
   _7.grid(row=4, column=0)
   _8 = Button(window, text='8', fg='white', bg='black', bd=0, command=lambda: input_number(8, equation), height=2, width=7)
   _8.grid(row=4, column=1)
   _9 = Button(window, text='9', fg='white', bg='black', bd=0, command=lambda: input_number(9, equation), height=2, width=7)
   _9.grid(row=4, column=2)
   _0 = Button(window, text='0', fg='white', bg='black', bd=0, command=lambda: input_number(0, equation), height=2, width=7)
   _0.grid(row=5, column=0)
   plus = Button(window, text='+', fg='white', bg='black', bd=0, command=lambda: input_number('+', equation), height=2, width=7)
   plus.grid(row=2, column=3)
   minus = Button(window, text='-', fg='white', bg='black', bd=0, command=lambda: input_number('-', equation), height=2, width=7)
   minus.grid(row=3, column=3)
   multiply = Button(window, text='*', fg='white', bg='black', bd=0, command=lambda:  input_number('*', equation), height=2, width=7)
   multiply.grid(row=4, column=3)
   divide = Button(window, text='/', fg='white', bg='black', bd=0, command=lambda: input_number('/', equation), height=2, width=7)
   divide.grid(row=5, column=3)
   equal = Button(window, text='=', fg='white', bg='black', bd=0, command=lambda: evaluate(equation), height=2, width=7)
   equal.grid(row=5, column=2)
   clear = Button(window, text='Clear', fg='white', bg='black', bd=0, command=lambda: clear_input_field(equation), height=2, width=7)
   clear.grid(row=5, column=1)
   # showing the GUI
   window.mainloop()
# start of the program
if __name__ == '__main__':
      main()

输出

运行上述程序,您将得到一个简单的计算器,如下所示。

上述表达式在 pression = button 后生成的结果。

结论

如果您对本教程有任何疑问,请在评论部分中提及。


相关文章