Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Ratio Calculator GUI using Tkinter
In this article, we will see how to create a functional GUI application that calculates ratios using Python's Tkinter library. We will use the Spinbox widget that creates an ideal spinner for numeric values. The Spinbox allows users to select values within a specified range by either typing directly or using up/down arrows.
We'll build a ratio calculator that solves for the fourth term in a proportion: if a:b = c:x, then x = (b×c)/a.
Creating the Ratio Calculator
Let's create a complete tkinter application with spinboxes for input values and a button to perform the calculation ?
from tkinter import *
# Create main window
win = Tk()
win.title("Ratio Calculator")
win.geometry("600x500")
win.resizable(0, 0)
# Create title label
label = Label(win, text="Ratio Calculator", font=('Times New Roman', 25))
label.pack(pady=20)
# Create instruction label
instruction = Label(win, text="Find X in ratio A:B = C:X", font=('Times New Roman', 16))
instruction.pack(pady=10)
# Define the function to calculate the ratio
def ratio_cal():
try:
a1 = int(a.get())
b1 = int(b.get())
c1 = int(c.get())
if a1 == 0:
x_val.config(text="Error: Division by zero")
return
val = (b1 * c1) / a1
x_val.config(text=f"X = {val:.2f}")
except ValueError:
x_val.config(text="Error: Invalid input")
# Create frame for spinboxes and result
frame = Frame(win)
frame.pack(pady=30)
# Create labels for the ratio format
Label(frame, text="A:", font=('Times New Roman', 14)).grid(row=0, column=0, padx=5)
a = Spinbox(frame, from_=1, to=100000, font=('Times New Roman', 14), width=10)
a.grid(row=0, column=1, padx=10)
Label(frame, text=":", font=('Times New Roman', 18)).grid(row=0, column=2)
Label(frame, text="B:", font=('Times New Roman', 14)).grid(row=0, column=3, padx=5)
b = Spinbox(frame, from_=0, to=100000, font=('Times New Roman', 14), width=10)
b.grid(row=0, column=4, padx=10)
Label(frame, text="=", font=('Times New Roman', 18)).grid(row=0, column=5, padx=10)
Label(frame, text="C:", font=('Times New Roman', 14)).grid(row=0, column=6, padx=5)
c = Spinbox(frame, from_=0, to=100000, font=('Times New Roman', 14), width=10)
c.grid(row=0, column=7, padx=10)
Label(frame, text=":", font=('Times New Roman', 18)).grid(row=0, column=8)
# Result label
x_val = Label(win, text="X = ?", font=('Times New Roman', 18), fg="blue")
x_val.pack(pady=20)
# Calculate button
Button(win, text="Calculate", command=ratio_cal, borderwidth=3,
fg="white", bg="black", width=15, font=('Times New Roman', 14)).pack(pady=20)
# Start the GUI loop
win.mainloop()
How It Works
The ratio calculator implements the mathematical formula for proportions:
- Input: Three Spinbox widgets for values A, B, and C
- Formula: X = (B × C) / A
- Output: Calculated value displayed in a label
- Error Handling: Checks for division by zero and invalid inputs
Key Features
| Widget | Purpose | Configuration |
|---|---|---|
| Spinbox | Numeric input | Range 0-100000, width=10 |
| Label | Display result | Font: Times New Roman, size 18 |
| Button | Trigger calculation | Black background, white text |
Output
Running the above code creates a GUI-based Ratio Calculator with a clear layout showing the proportion A:B = C:X format ?
Conclusion
This Tkinter-based ratio calculator provides an intuitive GUI for solving proportion problems. The Spinbox widgets make it easy to input numeric values, while error handling ensures reliable operation for mathematical calculations.
