Python-Rechner

Starten Sie


Hallo, im vorherigen Artikel habe ich gezeigt, wie man ein Spiel in Python erstellt , und jetzt schauen wir uns an, wie man einen einfachen Taschenrechner in Python tkinter erstellt .



Erstellen Sie ein 485 x 550-Fenster. Abmessungen sind nicht wichtig, ich mochte diese. Wir weisen auch darauf hin, dass das Fenster nicht verändert wird.



from tkinter import * class Main(Frame): def __init__(self, root): super(Main, self).__init__(root) self.build() def build(self): pass def logicalc(self, operation): pass def update(): pass if __name__ == '__main__': root = Tk() root["bg"] = "#000" root.geometry("485x550+200+200") root.title("") root.resizable(False, False) app = Main(root) app.pack() root.mainloop() 

Großartig, mach weiter.


Schaltflächen erstellen


Erstellen Sie in der Erstellungsmethode die folgende Liste:


 btns = [ "C", "DEL", "*", "=", "1", "2", "3", "/", "4", "5", "6", "+", "7", "8", "9", "-", "+/-", "0", "%", "X^2" ] 


Er ist verantwortlich für alle Schaltflächen, die in unserem Fenster erscheinen.


Wir haben die Liste erstellt, gehen nun die Schleife durch und zeigen diese Schaltflächen an. Dazu schreiben Sie in der gleichen Methode Folgendes:



 x = 10 y = 140 for bt in btns: com = lambda x=bt: self.logicalc(x) Button(text=bt, bg="#FFF", font=("Times New Roman", 15), command=com).place(x=x, y=y, width=115, height=79) x += 117 if x > 400: x = 10 y += 81 


Toll, wir haben Knöpfe. Fügen Sie die Beschriftung mit der Ausgabe hinzu. Ich möchte, dass der Text links ist, daher müssen keine Attribute für die Textausrichtung geschrieben werden.



 self.formula = "0" self.lbl = Label(text=self.formula, font=("Times New Roman", 21, "bold"), bg="#000", foreground="#FFF") self.lbl.place(x=11, y=50) 


Logik schreiben



 def logicalc(self, operation): if operation == "C": self.formula = "" elif operation == "DEL": self.formula = self.formula[0:-1] elif operation == "X^2": self.formula = str((eval(self.formula))**2) elif operation == "=": self.formula = str(eval(self.formula)) else: if self.formula == "0": self.formula = "" self.formula += operation self.update() def update(self): if self.formula == "": self.formula = "0" self.lbl.configure(text=self.formula) 


Da wir keine Tastatureingabe haben, können wir es uns leisten, einfach nach Sonderangeboten zu suchen. Tasten (C, DEL, =) und in anderen Fällen fügen Sie dies einfach der Formel hinzu.



Dieser Rechner hat viele Mängel, aber wir haben nicht versucht, ihn ideal zu machen.



Ich entschuldige mich für die Fehler im Artikel. Schreiben Sie, ich werde korrigieren.


Vollständiger Code meiner Version des Rechners:


 from tkinter import * class Main(Frame): def __init__(self, root): super(Main, self).__init__(root) self.build() def build(self): self.formula = "0" self.lbl = Label(text=self.formula, font=("Times New Roman", 21, "bold"), bg="#000", foreground="#FFF") self.lbl.place(x=11, y=50) btns = [ "C", "DEL", "*", "=", "1", "2", "3", "/", "4", "5", "6", "+", "7", "8", "9", "-", "(", "0", ")", "X^2" ] x = 10 y = 140 for bt in btns: com = lambda x=bt: self.logicalc(x) Button(text=bt, bg="#FFF", font=("Times New Roman", 15), command=com).place(x=x, y=y, width=115, height=79) x += 117 if x > 400: x = 10 y += 81 def logicalc(self, operation): if operation == "C": self.formula = "" elif operation == "DEL": self.formula = self.formula[0:-1] elif operation == "X^2": self.formula = str((eval(self.formula))**2) elif operation == "=": self.formula = str(eval(self.formula)) else: if self.formula == "0": self.formula = "" self.formula += operation self.update() def update(self): if self.formula == "": self.formula = "0" self.lbl.configure(text=self.formula) if __name__ == '__main__': root = Tk() root["bg"] = "#000" root.geometry("485x550+200+200") root.title("") root.resizable(False, False) app = Main(root) app.pack() root.mainloop() 

Source: https://habr.com/ru/post/de481074/


All Articles