Creating a scientific calculator in Python involves building a user interface to handle
various mathematical operations and functions. You can use the `tkinter` library to create
the graphical user interface and implement the calculator's functionality.
Here's a basic example of how to create a scientific calculator using `tkinter`:
1. **Import Libraries:**
Import the necessary libraries, including `tkinter` for GUI components and
`math` for mathematical operations.
2. **Create GUI:**
Build the calculator's user interface with buttons for digits, basic operators,
and scientific functions.
3. **Implement Functionality:**
Define functions for handling button clicks, performing calculations,
and displaying results.
4. **Run Application:**
Run the `tkinter` event loop to start the calculator application.
```python Program : -
```
- import tkinter as tk
- import math
- class ScientificCalculatorApp:
- def __init__(self, root):
- self.root = root
- self.root.title("Scientific Calculator")
- self.current_input = ""
- self.input_label = tk.Label(root, text="", font=("Helvetica", 24))
- self.input_label.pack(pady=10)
- self.buttons = [
- "7", "8", "9", "/",
- "4", "5", "6", "*",
- "1", "2", "3", "-",
- "0", ".", "=", "+",
- "sin", "cos", "tan",
- "sqrt", "log", "exp",
- ]
- self.create_buttons()
- def create_buttons(self):
- for button_text in self.buttons:
- button = tk.Button(self.root, text=button_text, font=("Helvetica", 18), command=lambda text=button_text: self.on_button_click(text))
- button.pack(side=tk.LEFT, padx=10, pady=10)
- def on_button_click(self, text):
- if text == "=":
- try:
- result = eval(self.current_input)
- self.current_input = str(result)
- self.update_input_label()
- except:
- self.current_input = "Error"
- self.update_input_label()
- elif text == "sqrt":
- self.current_input = str(math.sqrt(float(self.current_input)))
- self.update_input_label()
- elif text == "log":
- self.current_input = str(math.log10(float(self.current_input)))
- self.update_input_label()
- elif text == "exp":
- self.current_input = str(math.exp(float(self.current_input)))
- self.update_input_label()
- else:
- self.current_input += text
- self.update_input_label()
- def update_input_label(self):
- self.input_label.config(text=self.current_input)
- if __name__ == "__main__":
- root = tk.Tk()
- app = ScientificCalculatorApp(root)
- root.mainloop()
This example creates a basic scientific calculator with buttons for digits, basic operators,
and some common scientific functions like sine, cosine, tangent, square root, logarithm, and
exponential. The calculator evaluates expressions when the "=" button is pressed and
displays the result.
Feel free to enhance this example by adding more scientific functions, handling corner cases,
and improving the user interface.-09; ṃ/
Scientific calculator using python
Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated