Discuss / Python / 魔改样例代码编写了一个GUI版本的计算器~

魔改样例代码编写了一个GUI版本的计算器~

Topic source

遥望君山

#1 Created at ... [Delete] [Delete and Lock User]
from tkinter import *
import tkinter.messagebox as messagebox


class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()


    def createWidgets(self):
        self.expression = Entry(self)
        self.expression.pack()
        self.alertButton = Button(self, text='Evaluate it!', command=self.compute)
        self.alertButton.pack()


    def compute(self):
        expression = self.expression.get() or '0'
        messagebox.showinfo('Result', '%s = %s' % (expression, eval(expression)))


app = Application()
# 设置窗口标题:
app.master.title("Please input math expression")
# 主消息循环:
app.mainloop()

  • 1

Reply