|
| 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 | +#Add an inscription with the output of the result |
| 10 | + def build(self): |
| 11 | + self.formula = "0" |
| 12 | + self.lbl = Label(text=self.formula, font=("Times New Roman", 21, "bold"), bg="#000", foreground="#FFF") |
| 13 | + self.lbl.place(x=11, y=50) |
| 14 | +#Creating buttons |
| 15 | + btns = [ |
| 16 | + "C", "DEL", "*", "=", |
| 17 | + "1", "2", "3", "/", |
| 18 | + "4", "5", "6", "+", |
| 19 | + "7", "8", "9", "-", |
| 20 | + "(", "0", ")", "X^2" |
| 21 | + ] |
| 22 | +#We creating list, and after going with cycle and showing the buttons |
| 23 | + x = 10 |
| 24 | + y = 140 |
| 25 | + for bt in btns: |
| 26 | + com = lambda x=bt: self.logicalc(x) |
| 27 | + Button(text=bt, bg="#FFF", |
| 28 | + font=("Times New Roman", 15), |
| 29 | + command=com).place(x=x, y=y, |
| 30 | + width=115, |
| 31 | + height=79) |
| 32 | + x += 117 |
| 33 | + if x > 400: |
| 34 | + x = 10 |
| 35 | + y += 81 |
| 36 | +#Writing logic |
| 37 | + def logicalc(self, operation): |
| 38 | + if operation == "C": |
| 39 | + self.formula = "" |
| 40 | + elif operation == "DEL": |
| 41 | + self.formula = self.formula[0:-1] |
| 42 | + elif operation == "X^2": |
| 43 | + self.formula = str((eval(self.formula))**2) |
| 44 | + elif operation == "=": |
| 45 | + self.formula = str(eval(self.formula)) |
| 46 | + else: |
| 47 | + if self.formula == "0": |
| 48 | + self.formula = "" |
| 49 | + self.formula += operation |
| 50 | + self.update() |
| 51 | + |
| 52 | + def update(self): |
| 53 | + if self.formula == "": |
| 54 | + self.formula = "0" |
| 55 | + self.lbl.configure(text=self.formula) |
| 56 | + |
| 57 | +#Creating window 485 x 550 |
| 58 | +if __name__ == '__main__': |
| 59 | + root = Tk() |
| 60 | + root["bg"] = "#000" |
| 61 | + root.geometry("485x550+200+200") |
| 62 | + root.title("calculatort") |
| 63 | + root.resizable(False, False) |
| 64 | + app = Main(root) |
| 65 | + app.pack() |
| 66 | + root.mainloop() |
0 commit comments