Why can't I get the data of an entry and use it in a sentence?

66 Views Asked by At

I did most of the job, but when I enter the variables that I want to calculate, it doesn't work. There is a problem with the function part and below. Can someone please help me? Thanks

from tkinter import * 

win = Tk()
win.title("Age Generator")
win.geometry("600x300")
win.resizable(FALSE,FALSE)

etiket = Label(text="Welcome to the age generator.\nThis app, calculates your age with the year that you will choose",font="Cambria 12",fg="gray",anchor=CENTER)
etiket.place(relx=0.1,rely=0.1)

yaz = Label(text="Enter your age:",font="Cambria 10",bg="lightgray")
yaz.place(relx=0.2,rely=0.3)

age = Entry()
age.place(relx=0.4,rely=0.3)

yaz2 = Label(text="Ener the year:",font="Cambria 10",bg="lightgray")
yaz2.place(relx=0.2,rely=0.4)

year = Entry()
year.place(relx=0.4,rely=0.4)


def get_data():
    cf1 = etiket.config(text= age.get(), font= ('Helvetica 13'))
    cf2 = Label.config(text= year.get(), font= ('Helvetica 13'))
    # label= Label(text="", font=('Helvetica 13'))
    # label.pack()
    result = Label(text=f"{year} yılında {cf2 - (2023-cf1)} yaşında olacaksınız!",font="Cambria Bold 10",bg="darkgray",fg="black",anchor=CENTER)
    result.pack(side=BOTTOM)

button = Button(text="Calculate My Age",command=get_data,width=15,height=2)
button.place(relx=0.3,rely=0.5)

mainloop()

I was trying to do an application that tells you your age with the year that you choose. For example assume that I'm 20 and I'm asking which age I will be in 3786. The Machine answers it by the codes that I wrote.

But however, I always get TypeError.

1

There are 1 best solutions below

6
JRiggles On

The problem is that cf1 is the result of the call to config, not an integer. The same can be said for cf2. Because of this, f"{year} yılında {cf2 - (2023-cf1) won't work because you're trying to do math on Labels instead of numbers.

You can fix this by separating the getting of each value from the configuration of cf1 and cf2

def get_data():
    age_value = age.get()
    year_value = year.get()

    cf1 = etiket.config(text=age_value, font=('Helvetica 13'))
    cf2 = Label(text=year_value, font=('Helvetica 13'))
    cf2.pack(side=BOTTOM)

    result = Label(
        text=f"{year_value} yılında {int(year_value) - (2023 - int(age_value))} yaşında olacaksınız!",
        font="Cambria Bold 10",
        bg="darkgray",
        fg="black", anchor=CENTER
    )
    result.pack(side=BOTTOM)