Search code examples
pythonpython-3.xtkintercalculator

Tkinter Grid problem for function calculator


I just learned python and was experimenting with tkinter for a calculator program and my formula bar does not show up. I have tried innumerous things including asking ChatGPT, but still no success.

import tkinter as tk
import math
from sympy import *

root = tk.Tk()
root.geometry("400x500")
# root.resizable(False, False)


equation = ""
formula_bar = tk.Label(root, text="", width=45, height=2, relief="groove")
formula_bar.grid(row=0, column=0, columnspan=6)


def add_to_equation(value):
    global equation
    equation += value
    formula_bar.config(text=equation)


def evaluate_equation():
    global equation
    try:
        result = eval(equation)
        formula_bar.config(text=result)
    except:
        formula_bar.config(text="Error")
    equation = ""


def clear_equation():
    global equation
    equation = ""
    formula_bar.config(text="")


def add_pi():
    add_to_equation("3.141")


def add_euler():
    add_to_equation("2.718")


def add_phi():
    add_to_equation("1.618")


addition_button = tk.Button(root, text='+', command=lambda: add_to_equation('+'), width=8, height=4)
subtraction_button = tk.Button(root, text='-', command=lambda: add_to_equation('-'), width=8, height=4)
multiplication_button = tk.Button(root, text='*', command=lambda: add_to_equation('*'), width=8, height=4)
division_button = tk.Button(root, text='÷', command=lambda: add_to_equation('/'), width=8, height=4)
modulo_button = tk.Button(root, text='%', command=lambda: add_to_equation('%'), width=8, height=4)
power_button = tk.Button(root, text='power', command=lambda: add_to_equation('**'), width=8, height=4)
logarithm_button = tk.Button(root, text='log', command=lambda: add_to_equation('log('), width=8, height=4)
natural_logarithm_button = tk.Button(root, text='ln', command=lambda: add_to_equation('ln('), width=8, height=4)
factorial_button = tk.Button(root, text='!', command=lambda: add_to_equation('factorial('), width=8, height=4)
sine_button = tk.Button(root, text='sin', command=lambda: add_to_equation('sin('), width=8, height=4)
cosine_button = tk.Button(root, text='cos', command=lambda: add_to_equation('cos('), width=8, height=4)
tangent_button = tk.Button(root, text='tan', command=lambda: add_to_equation('tan('), width=8, height=4)
arcsine_button = tk.Button(root, text='arcsin', command=lambda: add_to_equation('arcsin('), width=8, height=4)
arccosine_button = tk.Button(root, text='arccos', command=lambda: add_to_equation('arccos('), width=8, height=4)
arctangent_button = tk.Button(root, text='arctan', command=lambda: add_to_equation('arctan('), width=8, height=4)
absolute_value_button = tk.Button(root, text='abs', command=lambda: add_to_equation('abs('), width=8, height=4)
differentiation_button = tk.Button(root, text='diff', command=lambda: add_to_equation('diff('), width=8, height=4)
integration_button = tk.Button(root, text='integrate', command=lambda: add_to_equation('integrate('), width=8, height=4)
x_button = tk.Button(root, text='x', command=lambda: add_to_equation('x'), width=8, height=4)
y_button = tk.Button(root, text='y', command=lambda: add_to_equation('y'), width=8, height=4)
zero_button = tk.Button(root, text='0', command=lambda: add_to_equation('0'), width=8, height=4)
one_button = tk.Button(root, text='1', command=lambda: add_to_equation('1'), width=8, height=4)
two_button = tk.Button(root, text='2', command=lambda: add_to_equation('2'), width=8, height=4)
three_button = tk.Button(root, text='3', command=lambda: add_to_equation('3'), width=8, height=4)
four_button = tk.Button(root, text='4', command=lambda: add_to_equation('4'), width=8, height=4)
five_button = tk.Button(root, text='5', command=lambda: add_to_equation('5'), width=8, height=4)
six_button = tk.Button(root, text='6', command=lambda: add_to_equation('6'), width=8, height=4)
seven_button = tk.Button(root, text='7', command=lambda: add_to_equation('7'), width=8, height=4)
eight_button = tk.Button(root, text='8', command=lambda: add_to_equation('8'), width=8, height=4)
nine_button = tk.Button(root, text='9', command=lambda: add_to_equation('9'), width=8, height=4)
point_button = tk.Button(root, text='.', command=lambda: add_to_equation('.'), width=8, height=4)
equal_button = tk.Button(root, text='=', command=evaluate_equation, width=8, height=4)
pi_button = tk.Button(root, text='pi', command=add_pi, width=8, height=4)
euler_button = tk.Button(root, text='e', command=add_euler, width=8, height=4)
phi_button = tk.Button(root, text='Φ', command=add_phi, width=8, height=4)
clear_button = tk.Button(root, text='C', command=clear_equation, width=8, height=4)

addition_button.grid(row=2,column=5)
subtraction_button.grid(row=3,column=5)
multiplication_button.grid(row=4,column=5)
division_button.grid(row=5,column=5)
modulo_button.grid(row=5,column=1)
power_button.grid(row=0,column=5)
logarithm_button.grid(row=0,column=6)
natural_logarithm_button.grid(row=1,column=6)
factorial_button.grid(row=2,column=6)
sine_button.grid(row=0,column=2)
cosine_button.grid(row=0,column=3)
tangent_button.grid(row=0,column=4)
arcsine_button.grid(row=1,column=2)
arccosine_button.grid(row=1,column=3)
arctangent_button.grid(row=1,column=4)
absolute_value_button.grid(row=3,column=6)
differentiation_button.grid(row=4,column=6)
integration_button.grid(row=5,column=6)
x_button.grid(row=0,column=1)
y_button.grid(row=1,column=1)
zero_button.grid(row=5,column=3)
one_button.grid(row=4,column=2)
two_button.grid(row=4,column=3)
three_button.grid(row=4,column=4)
four_button.grid(row=3,column=2)
five_button.grid(row=3,column=3)
six_button.grid(row=3,column=4)
seven_button.grid(row=2,column=2)
eight_button.grid(row=2,column=3)
nine_button.grid(row=2,column=4)
point_button.grid(row=5,column=2)
equal_button.grid(row=5,column=4)
pi_button.grid(row=2,column=1)
euler_button.grid(row=3,column=1)
phi_button.grid(row=4,column=1)
clear_button.grid(row=1,column=5)

root.mainloop()

This is all the code I am working with. I want the formula bar to be on the top and all the buttons below


Solution

  • Your formula_bar is on row=0, column=0, columnspan 6; however, you're placing several other items on row 0 in columns 1-6 so you're replacing the formula_bar with those widgets.