|
| 1 | +""" |
| 2 | +
|
| 3 | +wanna try some GUI based calculator, here is one! |
| 4 | +Try it, brake it. If you find some bug and have better way ahead, i welcome your change :) |
| 5 | +
|
| 6 | +Install dependencies: |
| 7 | + pip install kivy==2.3.1 kivymd==1.1.1 |
| 8 | +
|
| 9 | +""" |
| 10 | + |
| 11 | +from kivymd.app import MDApp |
| 12 | +from kivymd.uix.button import MDFlatButton # Fixed F403 |
| 13 | +from kivy.lang import Builder |
| 14 | + |
| 15 | +opt = ["(", ")", "X", "/", "+", "-"] |
| 16 | +opt_check = ["X", "/", "+", "-"] |
| 17 | + |
| 18 | +cal = """ |
| 19 | +MDScreen: |
| 20 | + MDBoxLayout: |
| 21 | + orientation:'vertical' |
| 22 | + MDLabel: |
| 23 | + text:'I welcome you!' |
| 24 | + adaptive_height:True |
| 25 | + halign:'center' |
| 26 | +
|
| 27 | + MDTextField: |
| 28 | + id:field |
| 29 | + |
| 30 | + font_size:dp(60) |
| 31 | + pos_hint:{'top':1} |
| 32 | + size_hint_x:1 |
| 33 | + size_hint_y:.4 |
| 34 | + readonly:True |
| 35 | + multiline:True |
| 36 | +
|
| 37 | + MDGridLayout: |
| 38 | + id:grid |
| 39 | + cols:4 |
| 40 | +""" |
| 41 | + |
| 42 | + |
| 43 | +class calculator(MDApp): |
| 44 | + |
| 45 | + def build(self): |
| 46 | + self.theme_cls.theme_style = "Dark" |
| 47 | + return Builder.load_string(cal) |
| 48 | + |
| 49 | + def on_start(self): |
| 50 | + |
| 51 | + self.root.ids.grid.add_widget( |
| 52 | + MDFlatButton(text="AC", on_release=self.delete_all, size_hint=[1, 1]) |
| 53 | + ) |
| 54 | + self.root.ids.grid.add_widget( |
| 55 | + MDFlatButton(text="del", on_release=self.delete, size_hint=[1, 1]) |
| 56 | + ) |
| 57 | + self.root.ids.grid.add_widget( |
| 58 | + MDFlatButton(text="^", on_release=self.to_field, size_hint=[1, 1]) |
| 59 | + ) |
| 60 | + |
| 61 | + for i in range(len(opt)): |
| 62 | + self.root.ids.grid.add_widget( |
| 63 | + MDFlatButton( |
| 64 | + text=opt[i], on_release=self.to_field_opt, size_hint=[1, 1] |
| 65 | + ) |
| 66 | + ) |
| 67 | + |
| 68 | + for i in range(10): |
| 69 | + self.root.ids.grid.add_widget( |
| 70 | + MDFlatButton(text=str(i), on_release=self.to_field, size_hint=[1, 1]) |
| 71 | + ) |
| 72 | + |
| 73 | + self.root.ids.grid.add_widget( |
| 74 | + MDFlatButton(text="=", on_release=self.calculate, size_hint=[1, 1]) |
| 75 | + ) |
| 76 | + |
| 77 | + def to_field(self, btn): |
| 78 | + if self.root.ids.field.text == "undefined": |
| 79 | + self.root.ids.field.text = "" |
| 80 | + self.root.ids.field.text = self.root.ids.field.text + btn.text |
| 81 | + |
| 82 | + def to_field_opt(self, btn): |
| 83 | + if self.root.ids.field.text == "undefined": |
| 84 | + self.root.ids.field.text = "" |
| 85 | + |
| 86 | + elif btn.text != "(" and btn.text != ")" and self.root.ids.field.text == "": |
| 87 | + self.root.ids.field.text = f"0+{btn.text}" |
| 88 | + |
| 89 | + elif self.root.ids.field.text != "" and btn.text in opt_check: |
| 90 | + if self.root.ids.field.text[-1] in opt_check: |
| 91 | + self.root.ids.field.text = self.root.ids.field.text[:-1] + btn.text |
| 92 | + else: |
| 93 | + self.root.ids.field.text = self.root.ids.field.text + btn.text |
| 94 | + |
| 95 | + else: |
| 96 | + self.root.ids.field.text = self.root.ids.field.text + btn.text |
| 97 | + |
| 98 | + def delete_all(self, del_all_btn): |
| 99 | + self.root.ids.field.text = "" |
| 100 | + |
| 101 | + def delete(self, del_btn): |
| 102 | + self.root.ids.field.text = self.root.ids.field.text[:-1] |
| 103 | + |
| 104 | + def calculate(self, cal_btn): |
| 105 | + ch_opt_list = ["X", "^"] |
| 106 | + with_opt = ["*", "**"] |
| 107 | + raw = self.root.ids.field.text |
| 108 | + |
| 109 | + for opt in ch_opt_list: |
| 110 | + raw = raw.replace(opt, with_opt[ch_opt_list.index(opt)]) |
| 111 | + |
| 112 | + try: |
| 113 | + self.root.ids.field.text = str(eval(raw)) |
| 114 | + except Exception: # Fixed E722 |
| 115 | + self.root.ids.field.text = "undefined" |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + calculator().run() |
0 commit comments