Python计算器

开始


您好,在上一篇文章中,我展示了如何使用python制作游戏 ,现在我们看一下如何使用python tkinter制作简单的计算器。



创建一个485 x 550窗口。尺寸并不重要,我喜欢这些。 我们还指示该窗口将不会更改。



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() 

太好了,继续前进。


制作按钮


构建方法中,创建以下列表:


 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 


太好了,我们有按钮。 在输出中添加题词。 我希望文本在左侧,因此,不需要编写文本对齐属性。



 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) 


写作逻辑



 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) 


因此,由于我们没有键盘输入,因此我们可以负担得起,只需检查特殊内容即可。 按钮(C,DEL,=),在其他情况下,只需将其添加到公式中即可。



该计算器有很多缺点,但我们并未尝试使其理想。



对于本文中的错误,我深表歉意。 写,我会纠正的。


我的计算器版本的完整代码:


 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/zh-CN481074/


All Articles