python+tkinter计算器

 

python+tkinter计算器_第1张图片

 

from tkinter import *
from tkinter.messagebox import *
root = Tk()
root.geometry('300x200')
root.title('计算器')




num = StringVar()
Entry(root, textvariable = num, bg = "yellow").place(relx=0, rely = 0, relwidth=1, relheight = 0.2)

def result():
    num.set(eval(num.get()))

Button(root, text = "1", command = lambda:num.set(num.get() + "1")).place(relx=0, rely = 0.2, relwidth=0.25, relheight = 0.2)
Button(root, text = "2", command = lambda:num.set(num.get() + "2")).place(relx=0.25, rely = 0.2, relwidth=0.25, relheight = 0.2)
Button(root, text = "3", command = lambda:num.set(num.get() + "3")).place(relx=0.5, rely = 0.2, relwidth=0.25, relheight = 0.2)
Button(root, text = "+", command = lambda:num.set(num.get() + "+")).place(relx=0.75, rely = 0.2, relwidth=0.25, relheight = 0.2)

Button(root, text = "4", command = lambda:num.set(num.get() + "4")).place(relx=0, rely = 0.4, relwidth=0.25, relheight = 0.2)
Button(root, text = "5", command = lambda:num.set(num.get() + "5")).place(relx=0.25, rely = 0.4, relwidth=0.25, relheight = 0.2)
Button(root, text = "6", command = lambda:num.set(num.get() + "6")).place(relx=0.5, rely = 0.4, relwidth=0.25, relheight = 0.2)
Button(root, text = "-", command = lambda:num.set(num.get() + "-")).place(relx=0.75, rely = 0.4, relwidth=0.25, relheight = 0.2)

Button(root, text = "7", command = lambda:num.set(num.get() + "7")).place(relx=0, rely = 0.6, relwidth=0.25, relheight = 0.2)
Button(root, text = "8", command = lambda:num.set(num.get() + "8")).place(relx=0.25, rely = 0.6, relwidth=0.25, relheight = 0.2)
Button(root, text = "9", command = lambda:num.set(num.get() + "9")).place(relx=0.5, rely = 0.6, relwidth=0.25, relheight = 0.2)
Button(root, text = "*", command = lambda:num.set(num.get() + "*")).place(relx=0.75, rely = 0.6, relwidth=0.25, relheight = 0.2)

Button(root, text = "0", command = lambda:num.set(num.get() + "0")).place(relx=0, rely = 0.8, relwidth=0.25, relheight = 0.2)
Button(root, text = ".", command = lambda:num.set(num.get() + ".")).place(relx=0.25, rely = 0.8, relwidth=0.25, relheight = 0.2)
Button(root, text = "=", command = result).place(relx=0.5, rely = 0.8, relwidth=0.25, relheight = 0.2)
Button(root, text = "/", command = lambda:num.set(num.get() + "/")).place(relx=0.75, rely = 0.8, relwidth=0.25, relheight = 0.2)

 

你可能感兴趣的:(python)