📜  Python | 使用Tkinter的简单GUI计算器

📅  最后修改于: 2020-05-02 07:07:07             🧑  作者: Mango

Python提供了多种开发GUI(图形用户界面)的选项。在所有GUI方法中,tkinter是最常用的方法。它是Python随附的Tk GUI工具包的标准Python接口。带有tkinter的Python输出了创建GUI应用程序的最快,最简单的方法。使用tkinter创建GUI很容易。
创建一个tkinter:

  1. 导入模块– tkinter
  2. 创建主窗口(容器)
  3. 将任意数量的小部件添加到主窗口
  4. 将事件触发器应用于小部件。

让我们使用Python Tkinter模块创建一个基于GUI的简单计算器,该计算器可以执行基本的算术运算加,减,乘和除运算。
下面是实现:

# 使用Tkinter创建简单的GUI计算器的Python程序
# 从tkinter模块导入所有内容
from tkinter import *
# 全局声明表达式变量
expression = ""
# 在文本输入框中更新表达式的功能
def press(num):
    # 指出全局表达式变量
    global expression
    # 字符串的串联
    expression = expression + str(num)
    # 使用set方法更新表达式
    equation.set(expression)
# 评估最终表达的功能
def equalpress():
    # Try and except语句用于处理诸如零除错误等错误。
    # 将该代码放入try块中,这可能会产生错误
    try:
        global expression
        # eval函数计算表达式,str函数将结果转换为字符串
        total = str(eval(expression))
        equation.set(total)
        # 用空字符串初始化表达式变量
        expression = ""
    # 如果产生错误,则由except块处理
    except:
        equation.set(" error ")
        expression = ""
# 清除文本输入框内容的功能
def clear():
    global expression
    expression = ""
    equation.set("")
# 驱动程式码
if __name__ == "__main__":
    # 创建一个GUI窗口
    gui = Tk()
    # 设置GUI窗口的背景色
    gui.configure(background="light green")
    # 设置GUI窗口的标题
    gui.title("Simple Calculator")
    # 设置GUI窗口的配置
    gui.geometry("265x125")
    # StringVar()是变量类,我们创建了该类的实例
    equation = StringVar()
    # 为显示表达式的创建文本输入框 .
    expression_field = Entry(gui, textvariable=equation)
    # 网格方法用于将小部件放置在表格状结构的各个位置 .
    expression_field.grid(columnspan=4, ipadx=70)
    equation.set('enter your expression')
    # 创建一个Buttons并放置在根窗口内的特定位置。
    # 当用户按下按钮时,将执行该按钮所属的命令或功能 .
    button1 = Button(gui, text=' 1 ', fg='black', bg='red',
                     command=lambda: press(1), height=1, width=7)
    button1.grid(row=2, column=0)
    button2 = Button(gui, text=' 2 ', fg='black', bg='red',
                     command=lambda: press(2), height=1, width=7)
    button2.grid(row=2, column=1)
    button3 = Button(gui, text=' 3 ', fg='black', bg='red',
                     command=lambda: press(3), height=1, width=7)
    button3.grid(row=2, column=2)
    button4 = Button(gui, text=' 4 ', fg='black', bg='red',
                     command=lambda: press(4), height=1, width=7)
    button4.grid(row=3, column=0)
    button5 = Button(gui, text=' 5 ', fg='black', bg='red',
                     command=lambda: press(5), height=1, width=7)
    button5.grid(row=3, column=1)
    button6 = Button(gui, text=' 6 ', fg='black', bg='red',
                     command=lambda: press(6), height=1, width=7)
    button6.grid(row=3, column=2)
    button7 = Button(gui, text=' 7 ', fg='black', bg='red',
                     command=lambda: press(7), height=1, width=7)
    button7.grid(row=4, column=0)
    button8 = Button(gui, text=' 8 ', fg='black', bg='red',
                     command=lambda: press(8), height=1, width=7)
    button8.grid(row=4, column=1)
    button9 = Button(gui, text=' 9 ', fg='black', bg='red',
                     command=lambda: press(9), height=1, width=7)
    button9.grid(row=4, column=2)
    button0 = Button(gui, text=' 0 ', fg='black', bg='red',
                     command=lambda: press(0), height=1, width=7)
    button0.grid(row=5, column=0)
    plus = Button(gui, text=' + ', fg='black', bg='red',
                  command=lambda: press("+"), height=1, width=7)
    plus.grid(row=2, column=3)
    minus = Button(gui, text=' - ', fg='black', bg='red',
                   command=lambda: press("-"), height=1, width=7)
    minus.grid(row=3, column=3)
    multiply = Button(gui, text=' * ', fg='black', bg='red',
                      command=lambda: press("*"), height=1, width=7)
    multiply.grid(row=4, column=3)
    divide = Button(gui, text=' / ', fg='black', bg='red',
                    command=lambda: press("/"), height=1, width=7)
    divide.grid(row=5, column=3)
    equal = Button(gui, text=' = ', fg='black', bg='red',
                   command=equalpress, height=1, width=7)
    equal.grid(row=5, column=2)
    clear = Button(gui, text='Clear', fg='black', bg='red',
                   command=clear, height=1, width=7)
    clear.grid(row=5, column='1')
    # start the GUI
    gui.mainloop()

输出: