Building a Number Addition GUI Application with Python's Tkinter
Import the Tkinter module to create a graphical user interface.
import tkinter as tk
Initialize the main application window.
app_window = tk.Tk()
app_window.title('Number Adder')
app_window.geometry('500x500')
Add a label to guide user input.
input_label = tk.Label(app_window, text='Enter numbers separated by commas:', width=45, font=('Arial', 22))
input_label.place(x=10, y=20)
Create an entry field for user input.
entry_label = tk.Label(app_window, text='Input values:')
entry_label.place(x=20, y=60)
input_entry = tk.Entry(app_window, width=25)
input_entry.place(x=150, y=60)
Add a label to display the calculation result.
result_label = tk.Label(app_window, text='Result:', width=45, font=('Arial', 22))
result_label.place(x=10, y=100)
Use a varialbe to store and update the result dynamically.
result_var = tk.DoubleVar()
result_display = tk.Label(app_window, textvariable=result_var, width=45)
result_display.place(x=150, y=230)
prefix_label = tk.Label(app_window, text='Sum:', width=10, font=('Arial', 18))
prefix_label.place(x=148, y=230)
Define a fucntion to compute the sum of entered numbers.
def calculate_sum():
total = 0
values = input_entry.get().split(',')
for num in values:
total += int(num)
result_var.set(total)
Add a button to trigger the calculation.
calc_button = tk.Button(app_window, text='Compute Sum', width=30, command=calculate_sum, font=('Arial', 22))
calc_button.place(x=90, y=180)
Start the GUI event loop.
app_window.mainloop()