Tkinter design using a simple addition calculator:
(1) In the digital input box, only numbers, other characters can not be entered when the input other characters will not be displayed and automatically remove these non-numeric characters in the input box.
(2) Results box read-only, so that the results can not be human to interference.
code show as below:
from tkinter import * root=Tk() # 计算器 frame=Frame(root) frame.pack(padx=10,pady=10) v1=StringVar() v2=StringVar() v3=StringVar() def test(content): return content.isdigit() testcmd=root.register(test) e1=Entry(frame,width=10,textvariable=v1,validate="key",\ validatecommand=(testcmd,'%P')).grid(row=0,column=0) Label(frame,text="+").grid(row=0,column=1) e2=Entry(frame,width=10,textvariable=v2,validate="key",\ validatecommand=(testcmd,'%P')).grid(row=0,column=2) Label(frame,text="=").grid(row=0,column=3) e3=Entry(frame,width=10,textvariable=v3,state="readonly").grid(row=0,column=4) def calc(): result=int(v1.get())+int(v2.get()) v3.set(str(result)) Button(frame,text="计算结果",command=calc).grid(row=1,column=2,pady=5) mainloop()
Operating results as follows: