This article introduces the Python-based Tkinter implementation of the simple calculator detailed code, share to everyone for your reference, the specific content is as follows
The first type: use Python's tkinter to implement a simple calculator
#coding: Utf-8from Tkinter import *import timeroot = Tk () def cacl (INPUT_STR): If "x" in Input_str:ret = Input_str.split ("x") return int (ret[0]) * INT (ret[1]) def callback (n):p rint ndef callback1 (n):p rint nclass app:def __init__ (self, Master): Frame1 = FRAME (master) frame1.pack () frame = FRAME (master) frame.pack () button (frame, text= "1", Command=lambda:callback ( 1). Grid (row=0,column=0) button (frame, text= "2", Command=lambda:callback (2)). Grid (Row=0,column=1) button (frame, text= "3", Command=lambda:callback (3)). Grid (row=0,column=2) button (frame, text= "4", Command=lambda:callback (4)). Grid (row=1,column=0) button (frame, text= "5", Command=lambda:callback (5)). Grid (Row=1,column=1) button (frame, text= "6 ", Command=lambda:callback (6)). Grid (row=1,column=2) button (frame, text=" 7 ", Command=lambda:callback (7)). Grid (row=2 , column=0) button (frame, text= "8", Command=lambda:callback (8)). Grid (Row=2,column=1) button (frame, text= "9", command= Lambda:callback (9)). Grid (row=2,column=2) button (frame, text= "0", Command=lambda: Callback (0)). Grid (row=3,column=0) button (frame, text= "+", Command=lambda:callback1 ("+")). Grid (Row=3,column=1) button (frame, text= "-", Command=lambda:callback1 ("-")). Grid (row=3,column=2) button (frame, text= "*", Command=lambda: Callback1 ("*")). Grid (Row=4,column=1) button (frame, text= "/", Command=lambda:callback1 ("/")). Grid (row=4,column=2) button (frame, text= "=", Command=self.say_hi). Grid (row=4,column=0) w = Label (frame1,text= "input result") W.pack () SELF.E = Entry (frame1) Self.e.pack (padx=5) w1 = Label (frame1,text= "calculation result") W1.pack () v = stringvar () e1 = Entry (frame1, Textvariable=v) V.set ("") SELF.V = Ve1.pack () def say_hi (self):p rint "Hi there, everyone!", self.e.get () Input_str = Self.e.get () Self.v.set (CAcl (input_str)) app = app (root) Root.mainloop ()
The second type: Simple calculator based on Tkinter with 50 lines of Python code
Tkinter is usually python-brought, so the code does not need other components, this program is implemented in the python2.7 version.
Mainly involved in the use of tkinter, function definition and invocation, the use of anonymous functions, class member function definition, such as Python basic knowledge, suitable for beginners to learn.
The code is as follows:
From Tkinter import * #创建横条型框架 def frame (root, side): W = frame (root) w.pack (side = side, expand = YES, fill = BOTH) r Eturn w #创建按钮 def button (root, side, text, command = None): W = button (root, Text = text, command = command) W.pack (side = side, expand = YES, fill = BOTH) Return w #继承了Frame类, initialize the layout of the Program Interface class Calculator (Frame): def __init__ (self): Fra Me.__init__ (self) self.pack (expand = YES, fill = BOTH) self.master.title (' Simple calculater ') display = String Var () #添加输入框 Entry (self, relief = SUNKEN, textvariable = display). Pack (side = TOP, expand = YES, fill = BOTH) #添加横条型框架以及里面的按钮 for key in (' 123 ', ' 456 ', ' 789 ', ' -0. '): Keyf = frame (self, TOP) for Char in Key:butt On (Keyf, left, char, lambda w = display, c = Char:w.set (W.get () + c)) #添加操作符按钮 OPSF = frame (self, TOP) for Char in ' +-*/= ': if char = = ' = ': btn = button (OPSF, left, char) btn.bind ('
', lambda e, s = self, w = Display:s.calc (w), ' + ') else:btn = button (OPSF, left, char, lambda w = display, s = '%s '%char:w.set (W.get () + s)) #添加清除按钮 Clearf = frame (self, BOTTOM) button (Clearf, left, ' clear ', lambda w = dis Play:w.set (")) #调用eval函数计算表达式的值 def calc (self, display): Try:display.set (eval (Display.get ())) EXCEPT:DISPL Ay.set ("ERROR") #程序的入口 if __name__ = = ' __main__ ': print (' OK ') Calculator (). Mainloop ()
Implementation results such as:
The above is the whole content of this article, I hope that everyone's learning Python programming help.