tkinter计算字符串的值


from Tkinter import *

class App:

    def __init__(self,root):

        frame = Frame(root)
        frame.pack()
        self.frame = frame

        w = Label(frame,text = "calculator")
        w.pack()

        self.newinput()

        button1 = Button(frame,text='1',fg="red",command = lambda : self.buttoncb(1))
        button1.pack()

        button2 = Button(frame,text='2',fg="red",command = lambda : self.buttoncb(2))
        button2.pack()

        button3 = Button(frame,text='+',fg="red",command = lambda : self.buttoncb("+"))
        button3.pack()

        button4 = Button(frame,text='=',fg="red",command = lambda : self.cal())
        button4.pack()

        button = Button(frame,text='Quit',fg="red",command = root.quit)
        button.pack()

     def newinput(self):

        v = StringVar()
        e = Entry(self.frame,textvariable = v)
        self.v = v
        e.pack()

    def buttoncb(self,i):
        val = self.v.get()
        self.v.set(val+str(i))

    def cal(self):
        v = self.v.get()

        self.v.set(eval(v))


root = Tk()
a = App(root)
root.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值