└── calculator on python /calculator on python: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | 4 | class Main(Frame): 5 | def __init__(self, root): 6 | super(Main, self).__init__(root) 7 | self.build() 8 | 9 | def build(self): 10 | self.formula = "0" 11 | self.lbl = Label(text=self.formula, font=("Times New Roman", 21, "bold"), bg="#000", foreground="#FFF") 12 | self.lbl.place(x=11, y=50) 13 | 14 | btns = [ 15 | "C", "DEL", "*", "=", 16 | "1", "2", "3", "/", 17 | "4", "5", "6", "+", 18 | "7", "8", "9", "-", 19 | "(", "0", ")", "X^2" 20 | ] 21 | 22 | x = 10 23 | y = 140 24 | for bt in btns: 25 | com = lambda x=bt: self.logicalc(x) 26 | Button(text=bt, bg="#FFF", 27 | font=("Times New Roman", 15), 28 | command=com).place(x=x, y=y, 29 | width=115, 30 | height=79) 31 | x += 117 32 | if x > 400: 33 | x = 10 34 | y += 81 35 | 36 | def logicalc(self, operation): 37 | if operation == "C": 38 | self.formula = "" 39 | elif operation == "DEL": 40 | self.formula = self.formula[0:-1] 41 | elif operation == "X^2": 42 | self.formula = str((eval(self.formula))**2) 43 | elif operation == "=": 44 | self.formula = str(eval(self.formula)) 45 | else: 46 | if self.formula == "0": 47 | self.formula = "" 48 | self.formula += operation 49 | self.update() 50 | 51 | def update(self): 52 | if self.formula == "": 53 | self.formula = "0" 54 | self.lbl.configure(text=self.formula) 55 | 56 | 57 | if __name__ == '__main__': 58 | root = Tk() 59 | root["bg"] = "#000" 60 | root.geometry("485x550+200+200") 61 | root.title("Калькулятор") 62 | root.resizable(False, False) 63 | app = Main(root) 64 | app.pack() 65 | root.mainloop() 66 | --------------------------------------------------------------------------------