Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
Creating a simple GUI calculator using tkinter
is a fun way to get to know the library. Let's design a basic calculator with the standard operations: addition, subtraction, multiplication, and division.
tkinter
:1. Import Required Libraries:
import tkinter as tk
2. Logic for the Calculator:
Let's define basic functions for each operation:
def add(a, b): return a + b def subtract(a, b): return a - b def multiply(a, b): return a * b def divide(a, b): if b == 0: return "Error (div by 0)" return a / b
3. Design the GUI:
def on_click(event): # Update the input field when a button is clicked text = event.widget.cget("text") if text == "=": try: result_str = str(eval(str(entry.get()))) entry.delete(0, tk.END) entry.insert(tk.END, result_str) except Exception as e: entry.delete(0, tk.END) entry.insert(tk.END, "Error") elif text == "C": entry.delete(0, tk.END) else: entry.insert(tk.END, text) root = tk.Tk() root.title("Simple Calculator") # Create the entry box entry = tk.Entry(root, width=40, borderwidth=5) entry.pack(padx=10, pady=10) # Create button layout buttons = [ '7', '8', '9', '+', '4', '5', '6', '-', '1', '2', '3', '*', 'C', '0', '=', '/' ] # Create the buttons using a loop current_row = 1 for btn in buttons: tk.Button(root, text=btn, width=10, height=2).grid(row=current_row, column=(buttons.index(btn) % 4)) current_row += 1 if (buttons.index(btn) + 1) % 4 == 0 else 0 # Bind the buttons to the on_click function for btn in buttons: widget = root.grid_slaves(row=current_row-1, column=buttons.index(btn) % 4)[0] widget.bind("<Button-1>", on_click) if current_row != 1 and (buttons.index(btn) + 1) % 4 == 0: current_row -= 1 root.mainloop()
The on_click
function updates the input field when a button is clicked. When the "=" button is clicked, it tries to evaluate the expression in the input field. If it encounters an error, it displays "Error". The "C" button clears the input field.
This is a very basic calculator that only handles the standard operations. For more advanced operations, or to better handle input and operations (such as handling floats, integrating a "backspace" button, or incorporating scientific calculations), you can extend the above code and add more functionality.
Creating a basic calculator with Tkinter:
Description: This example covers the basic structure of a Tkinter-based calculator, including the main window, display area, and buttons for numbers and operations.
import tkinter as tk # Create main window root = tk.Tk() root.title("Basic Calculator") # Create display widget display = tk.Entry(root, width=20, font=('Arial', 14)) display.grid(row=0, column=0, columnspan=4) # Define button click function def button_click(value): current = display.get() display.delete(0, tk.END) display.insert(0, current + value) # Create number buttons for i in range(1, 10): btn = tk.Button(root, text=str(i), command=lambda i=i: button_click(str(i))) btn.grid(row=(i-1)//3 + 1, column=(i-1)%3) # Create operation buttons operations = ['+', '-', '*', '/'] for i, op in enumerate(operations): btn = tk.Button(root, text=op, command=lambda op=op: button_click(op)) btn.grid(row=i+1, column=3) # Create equals button equals_btn = tk.Button(root, text='=', command=lambda: button_click('=')) equals_btn.grid(row=4, column=0, columnspan=3) # Run the main loop root.mainloop()