From ba55b4099d916042cc2725537383ff0ccffe2fab Mon Sep 17 00:00:00 2001 From: aditya7balotra Date: Mon, 4 Aug 2025 23:53:51 +0530 Subject: [PATCH] 1. Add calculator app using Kivy. 2. Update README.md to include Kivy calculator app. --- README.md | 3 +- kivy_calculator.py | 119 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 kivy_calculator.py diff --git a/README.md b/README.md index 873ea61f1b9..9ae51148977 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ Feel free to explore the scripts and use them for your learning and automation n 40. [Test Case Generator](https://github.com/Tanmay-901/test-case-generator/blob/master/test_case.py) - Generate different types of test cases with a clean and friendly UI, used in competitive programming and software testing. 41. [Extract Thumbnail From Video](https://github.com/geekcomputers/Python/tree/ExtractThumbnailFromVideo) - Extract Thumbnail from video files 42. [How to begin the journey of open source (first contribution)](https://www.youtube.com/watch?v=v2X51AVgl3o) - First Contribution of open source +43. [kivy_calculator.py](https://github.com/geekcomputers/Python/blob/master/kivy_calculator.py) - Calculator UI using Kivy
-_**Note**: The content in this repository belongs to the respective authors and creators. I'm just providing a formatted README.md for better presentation._ +_**Note**: The content in this repository belongs to the respective authors and creators. I'm just providing a formatted README.md for better presentation._ \ No newline at end of file diff --git a/kivy_calculator.py b/kivy_calculator.py new file mode 100644 index 00000000000..1e380c3c030 --- /dev/null +++ b/kivy_calculator.py @@ -0,0 +1,119 @@ +""" + +wanna try some GUI based calculator, here is one! +Try it, brake it. If you find some bug and have better way ahead, i welcome your change :) + +Install dependencies: + pip install kivy==2.3.1 kivymd==1.1.1 + +""" + +from kivymd.app import MDApp +from kivymd.uix.button import MDFlatButton # Fixed F403 +from kivy.lang import Builder + +opt = ["(", ")", "X", "/", "+", "-"] +opt_check = ["X", "/", "+", "-"] + +cal = """ +MDScreen: + MDBoxLayout: + orientation:'vertical' + MDLabel: + text:'I welcome you!' + adaptive_height:True + halign:'center' + + MDTextField: + id:field + + font_size:dp(60) + pos_hint:{'top':1} + size_hint_x:1 + size_hint_y:.4 + readonly:True + multiline:True + + MDGridLayout: + id:grid + cols:4 +""" + + +class calculator(MDApp): + + def build(self): + self.theme_cls.theme_style = "Dark" + return Builder.load_string(cal) + + def on_start(self): + + self.root.ids.grid.add_widget( + MDFlatButton(text="AC", on_release=self.delete_all, size_hint=[1, 1]) + ) + self.root.ids.grid.add_widget( + MDFlatButton(text="del", on_release=self.delete, size_hint=[1, 1]) + ) + self.root.ids.grid.add_widget( + MDFlatButton(text="^", on_release=self.to_field, size_hint=[1, 1]) + ) + + for i in range(len(opt)): + self.root.ids.grid.add_widget( + MDFlatButton( + text=opt[i], on_release=self.to_field_opt, size_hint=[1, 1] + ) + ) + + for i in range(10): + self.root.ids.grid.add_widget( + MDFlatButton(text=str(i), on_release=self.to_field, size_hint=[1, 1]) + ) + + self.root.ids.grid.add_widget( + MDFlatButton(text="=", on_release=self.calculate, size_hint=[1, 1]) + ) + + def to_field(self, btn): + if self.root.ids.field.text == "undefined": + self.root.ids.field.text = "" + self.root.ids.field.text = self.root.ids.field.text + btn.text + + def to_field_opt(self, btn): + if self.root.ids.field.text == "undefined": + self.root.ids.field.text = "" + + elif btn.text != "(" and btn.text != ")" and self.root.ids.field.text == "": + self.root.ids.field.text = f"0+{btn.text}" + + elif self.root.ids.field.text != "" and btn.text in opt_check: + if self.root.ids.field.text[-1] in opt_check: + self.root.ids.field.text = self.root.ids.field.text[:-1] + btn.text + else: + self.root.ids.field.text = self.root.ids.field.text + btn.text + + else: + self.root.ids.field.text = self.root.ids.field.text + btn.text + + def delete_all(self, del_all_btn): + self.root.ids.field.text = "" + + def delete(self, del_btn): + self.root.ids.field.text = self.root.ids.field.text[:-1] + + def calculate(self, cal_btn): + ch_opt_list = ["X", "^"] + with_opt = ["*", "**"] + raw = self.root.ids.field.text + + for opt in ch_opt_list: + raw = raw.replace(opt, with_opt[ch_opt_list.index(opt)]) + + try: + self.root.ids.field.text = str(eval(raw)) + except Exception: # Fixed E722 + self.root.ids.field.text = "undefined" + + +if __name__ == "__main__": + calculator().run()