pythontkinterpythagorean

Tkinter button does not do what it should do


I am developing an app to calculate the missing side of a triangle using Pythagorean theorem.

I have 3 buttons that tell the program what side of the triangle is unknown(a, b or c). This should make 2 entries pop up with letters next to them that clarify what side the entry wants to know.

from tkinter import*
from tkinter.ttk import *
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import messagebox

Window = Tk()
#the line above creates an empty window
Window.configure(bg='light blue')

Window.geometry("450x250")
hi = tk.Label(
    text="Hello, this is an app to calculate the unknown side of a triangle.\nusing the pyhtagoras theorem\nyou know what pythagoras theorem is I persume, if not\ngoogle it",
    fg="black",
    bg="light blue",
    width=75,
    height=4)
hi.pack(side = TOP )
#variable hi is a label that sintroduces the user to the program


labela = tk.Label(text="a")
labelb = tk.Label(text="b")
labelc = tk.Label(text="c")
calculate_button=tk.Button(
    text="side b",
    width=10,
    height=2,
    bg="black",
    fg="light blue",)
entrya=tk.Entry()
entryb=tk.Entry()
entryc=tk.Entry()
def functiona():
    calculate_button.pack()
    entryb.pack()
    entryb.place(relx=.8,
                 rely=.4,
                 anchor="center")
    entryc.pack()
    entryc.place(relx=.8,
                 rely=.6,
                 anchor="center")
    labelb.pack()
    labelb.place(relx=.6,
                 rely=.4,
                 anchor="center")
    labelc.pack()
    labelc.place(relx=.6,
                 rely=.6,
                 anchor="center")
def functionb():
    entrya.pack()
    entrya.place(relx=.8,
                 rely=.4,
                 anchor="center")
    entryc.pack()
    entryc.place(relx=.8,
                 rely=.6,
                 anchor="center")
    labela.pack()
    labela.place(relx=.6,
                 rely=.4,
                 anchor="center")
    labelc.pack()
    labelc.place(relx=.6,
                 rely=.6,
                 anchor="center")
def functionc():
    entrya.pack()
    entrya.place(relx=.8,
                 rely=.4,
                 anchor="center")
    entryb.pack()
    entryb.place(relx=.8,
                 rely=.6,
                 anchor="center")
    labela.pack()
    labela.place(relx=.6,
                 rely=.4,
                 anchor="center")
    labelb.pack()
    labelb.place(relx=.6,
                 rely=.6,
                 anchor="center")
#functions that get called when you click te buttons       

button1 = tk.Button(
    command=functiona,
    text="side a",
    width=10,
    height=2,
    bg="black",
    fg="light blue",
)
button1.pack()
button1.place(relx=.3, rely=.8, anchor="e")
#button that tells the software side a is unknown

button2 = tk.Button(
    command=functionb,
    text="side b",
    width=10,
    height=2,
    bg="black",
    fg="light blue",
)
button2.pack()
button2.place(relx=.3, rely=.6, anchor="e")
#button that tells the software side b is unknown

button3 = tk.Button(
    command=functionc,
    text="side c",
    width=10,
    height=2,
    bg="black",
    fg="light blue",
)
button3.pack()
button3.place(relx=.3, rely=.4, anchor="e")
#button that tells the software side c is missings
Window.mainloop()

Solution

  • you are placing the labels and entries on every button press, but you are not removing the old labels, so the new labels might be placed below the old ones, and won't be visible.

    the 'proper' and organized workaround for larger applications is to use frames, and just switch frames on the fly, and you should check a tutorial about it.

    for your application, you can add this function that will clear the labels and entries:

    def clear_all():
        labela.place_forget()
        labelb.place_forget()
        labelc.place_forget()
        entrya.place_forget()
        entryb.place_forget()
        entryc.place_forget()
    

    and just call it in the first line inside your functions

    def functionb():
        clear_all()
        # rest of code here