Collection of float type data from an Entry Widget - Python Tkinter

Asked

Viewed 806 times

0

I am developing a program for civil engineering calculations and I have a problem in the result collided in an Entry Widget, that same Entry is with you in an if condition as shown in the code below, however, when I try to use the value typed in this field, the obtained result is always 0. I tried to collect as String and convert to float, but an error appears that it was not possible to convert. Error appears in Function confirm 1, if condition == 1.

# ...
if Vrd < Vd:

    bws = str("Utilize uma largura mínima de viga de %.f cm" % ((Vd / (0.2 * he * fcd)) * 100))
    fcks = str("Utilize um Fck mínimo de %.f MPa" % (1.4 * Vd / (200 * bw * he)))

    # Janela de verificação de cisalhamento
    verificacao1 = Tk()
    verificacao1.title("ERRO NA VERIFICAÇÃO")
    verificacao1.geometry("+300+100")

    # Frames
    frame_top1 = Frame(verificacao1)
    frame_top1.pack()
    frame_sugestoes = Frame(verificacao1)
    frame_sugestoes.pack(side=LEFT)

    def bt_click_nao1():
        verificacao1.destroy()
        return

    def bt_click_sim1():
        bt_sim1["state"] = "disabled"
        global raid11
        raid11 = IntVar()

        def ck1():
            ck_Afck["state"] = "disabled"
            en_fck["state"] = "disabled"
            global teste
            teste = 1
            return

        def ck2():
            ck_Alargura["state"] = "disabled"
            en_largura["state"] = "disabled"
            global teste
            teste = 2
            return

        def reset():
            ck_Alargura["state"] = "normal"
            ck_Afck["state"] = "normal"
            ck_Alargura.deselect()
            ck_Afck.deselect()
            en_largura["state"] = "normal"
            en_fck["state"] = "normal"
            en_largura.delete(first=0, last=11)
            en_fck.delete(first=0, last=11)
            return

        def confirmar1():
            verificacao1.destroy()

            if teste == 1:

                *bw1 = raid11.get() * 0.01*
                # Carregamento
                Q = (bw1 * h * 25) + Gi + Gs
                # print(f'#Novo Carregamento: \033[33m{Q:.2f} KN/m\033[m')

                # Esforços Solicitantes
                V = (Q * le) / 2
                M = (Q * pow(le, 2)) / 8
                # print(f'#Nova Cortante: \033[33m{V:.2f} KN\033[m')
                # print(f'#Novo Momento Máximo: \033[33m{M:.2f} KN.m\033[m')

                # Verificação do Concreto
                # Cisalhamento
                fcd = (fck * 1000) / 1.4
                Vrd = 0.20 * bw1 * he * fcd
                Vd = 1.4 * V
                print(f'#Nova cortante de cálculo: \033[33m{Vd:.2f} KN\033[m')
                print(f'#Nova resistênca de cálculo: \033[33m{Vrd:.2f} KN\033[m')

            if teste == 2:

                # Carregamento

                Q = (bw * h * 25) + Gi + Gs
                # print(f'#Novo Carregamento: \033[33m{Q:.2f} KN/m\033[m')

                # Esforços Solicitantes
                V = (Q * le) / 2
                M = (Q * pow(le, 2)) / 8
                # print(f'#Nova Cortante: \033[33m{V:.2f} KN\033[m')
                # print(f'#Novo Momento Máximo: \033[33m{M:.2f} KN.m\033[m')

                # Verificação do Concreto
                # Cisalhamento
                fcd = (fck * 1000) / 1.4
                vrd = 0.20 * bw * he * fcd
                Vd = 1.4 * V
                # print(f'#Nova cortante de cálculo: \033[33m{Vd:.2f} KN\033[m')
                # print(f'#Nova resistênca de cálculo: \033[33m{Vrd:.2f} KN\033[m')

                # Zona de Apoio
                Vd1 = Vd * 1.1
                Vrd1 = 0.8 * bw * (c1 + hf) * fcd
                print(f'Solicitação na zona de apoio: \033[33m{Vd1:.2f} KN\033[m')
                print(f'Resistência da zona de apoio: \033[33m{Vrd1:.2f} KN\033[m')
            return

        # textos e CheckButtons de escolha
        lb_escolaSolucao = Label(frame_sugestoes, text="ESCOLHA A SOLUÇÃO:", font=("Arial", 12, "bold"), pady=10,
                                 bd=3, fg="#1E90FF", anchor="center")
        lb_escolaSolucao.grid(row=0, column=3)
        ck_Alargura = Checkbutton(frame_sugestoes, text="Aumentar largura", font=("Segoe UI", 12), command=ck1)
        ck_Alargura.grid(row=1, column=3, sticky=W)
        ck_Afck = Checkbutton(frame_sugestoes, text="Aumentar o Fck", font=("Segoe UI", 12), command=ck2)
        ck_Afck.grid(row=2, column=3, sticky=W)

        # Entrada de dados
        en_largura = Entry(frame_sugestoes, font=("Century Gothic", 11), textvariable=raid11, bd=5, insertwidth=4,
                           width=12, bg="#1E90FF", justify="right")
        en_largura.grid(row=1, column=4)
        en_fck = Entry(frame_sugestoes, font=("Century Gothic", 11), textvariable=raid12, bd=5, insertwidth=4,
                       width=12, bg="#1E90FF", justify="right")
        en_fck.grid(row=2, column=4)

        # botões reset e confirmar
        bt_reset = Button(frame_sugestoes, text="RESET", font=("Arial", 12, "bold"), bg="#1E90FF", padx=16, pady=0,
                          bd=2, width=7, command=reset)
        bt_reset.grid(row=4, column=3, sticky=E)
        bt_confirmar1 = Button(frame_sugestoes, text="CONFIRMAR", font=("Arial", 12, "bold"), bg="#1E90FF", padx=16,
                               pady=0, bd=2, width=7, command=confirmar1)
        bt_confirmar1.grid(row=4, column=4, sticky=W)

    # Textos
    lb_alerta1 = Label(frame_top1, text="A ESTRUTURA NÃO SUPORTA O EFEITO DE CISALHAMENTO NO CONCRETO",
                       font=("Arial", 13, "bold"), pady=12, fg="#1E90FF")
    lb_alerta1.grid(row=0, column=0)
    lb_psugestoes = Label(frame_sugestoes, text="POSSÍVEIS SOLUÇÕES", font=("Arial", 12, "bold"), pady=10, bd=3,
                          fg="#1E90FF")
    lb_psugestoes.grid(row=0, column=0)
    lb_largura = Label(frame_sugestoes, text=bws, font=("Segoe UI", 12))
    lb_largura.grid(row=1, column=0, sticky=W)
    lb_fck = Label(frame_sugestoes, text=fcks, font=("Segoe UI", 12))
    lb_fck.grid(row=2, column=0, sticky=W)
    lb_quest1 = Label(frame_sugestoes, text="Deseja aplicar alguma solução? ", font=("Segoe UI", 12))
    lb_quest1.grid(row=4, column=0, sticky=W)
    lb_linha1 = Label(frame_sugestoes)
    lb_linha1.grid(row=3, column=0)

    # botões - SIM/NÃO
    bt_sim1 = Button(frame_sugestoes, text="SIM", font=("Arial", 12, "bold"), bg="#1E90FF", padx=8, pady=0, bd=2,
                     width=5, command=bt_click_sim1)
    bt_sim1.grid(row=4, column=0, sticky=E)
    bt_nao1 = Button(frame_sugestoes, text="NÃO", font=("Arial", 12, "bold"), bg="#1E90FF", padx=8, pady=0, bd=2,
                     width=5, command=bt_click_nao1)
    bt_nao1.grid(row=4, column=2, sticky=W)
    verificacao1.mainloop()

Well, explaining more about my doubt. This program does security checks, the if at the beginning declares that if the request is greater than the resistance the program must execute the entire (giant) block below. Well in this block is created a new small window that serves only to alert the user that there is an error and conveniently point a solution to the error, the user must enter a new value for the data that will fix the problem (Ex. 15cm error width, but with 18cm is enough, the user can type the 18cm or a higher value). The problem is that I’m unable to capture this new value. And in response to jsbueno, I’ve tried using float. And this * 0.01 is simply a multiplication of the captured value (user type in cm and the program does the calculations in m).

  • Good evening, Friend Voce could explain me this line? bw1 = raid11.get() * 0.01 I move a lot in Kinter, but I didn’t understand the asterisks at the beginning and at the end.

  • I don’t know what the purpose is for you to put this entire portion of the program into one if global - it is likely that you have learned to program on your own. It works, but it’s not a much-used practice (in fact, it’s so strange that it’s not even a "not recommended" practice, because I don’t think anyone would go out making code like that). Well, this does not affect your question or question, but it is the tip for a revision of the program. And it can drive away people who would try to cure their doubt. "Wow, this code is so crazy that I won’t even look".

  • @Junyorsilva: Tkinter does not interfere with Python syntax. This line will give syntax error - there is even a way to have * in an assignment, like "*a, b = (1,2,3)- nesse caso, "a" ficaria com o "começo da sequência exceto o último elemento"(1, 2)eb` would have 3. But in this case it’s just wrong syntax anyway.

1 answer

1

The .get will always return a string. If you want to use this content as decimal number, call float the value returned by .get() is the right thing to do.

Probably when you tried you must have missed something - for example, the line *bw1 = raid11.get() * 0.01* contains two syntax errors- the "*" as prefix variables for atrbuition even exists in Python, but when you need it, you will know what it is doing - not like in C, who depending on how the code is written, you may have a pointer or not and have to directly change the variable, or the content of the pointed position. In short: forget these asterisks.

Like the only line where there’s a get is just this, try:

bw1 = float(raid11.get()) * 0.01

More: Since you are using a global variable to pass information to your functions, put the global in the first row of all functions where the value of the variable is changed (and not immediately before using it). In Python, it’s a matter of style, but it’s a style well recommended. And, as I mentioned in the comment, it is quite strange to declare functions and build entire interfaces within the block of a if in the module body - it is best to always declare all functions unconditionally, and have a function that "controls" what happens -inside it may be that if, and it triggers or not the call of other functions to build parts of the interface.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.