0

This code was meant to calculate an hypothenuse value, but it isn't running, the application doesn't run it at all,I think the mistake is in the while loops:

from math import *
import tkinter as tk
from tkinter import *
from tkinter import ttk

root = tk.Tk()
#USER choosen sizes:
wwdimensions = 300
whdimensions = 300
#Class to get screen dimensions easier
class winfo:
    swidth = root.winfo_screenwidth()
    sheight = root.winfo_screenheight()
    def width(screenwidth,windowwidth):
        intendedposwidth = int(screenwidth/2 - windowwidth/2)
        return(intendedposwidth)
    def height(screenheight,windowheight):
        intendedposheight = int(screenheight/2 - windowheight/2)
        return(intendedposheight)
screenwidth=winfo.swidth
screenheight = winfo.sheight
inputcenterw=winfo.width(screenwidth,wwdimensions)
inputcenterh=winfo.height(screenheight,whdimensions)
root.geometry("{}x{}+{}+{}".format(wwdimensions,whdimensions,inputcenterw,inputcenterh))

root.title("Pythagoream Theorem")
v1 = tk.StringVar()
v2 = tk.StringVar()
val1 = v1.get()
val2 = v2.get()
st = "disabled"
while val1 or val2 == "":
    st = "disabled"
    la=tk.Label(root,text="Fill the entry spaces").pack(fill=X,ipady=10,side= "bottom")
else:
    st="disabled"
while val1 or val2 == str:
    try:
        val2 = float(val2)
        val1 = float(val1)
    except:
        lab=tk.Label(root,text="Invalid dimensions,please solve it").pack(ipady=10,side="bottom",fill= X)
        st = "disabled"
    else:
        st = "normal"
st = "normal"
def showmsg():
    result = tk.Label(root, text = f"The hypotenuse is {sqrt(float(val1)**2+float(val2)**2)}",font=("Arial 12"))
    result.pack(ipady=10,side="bottom",fill=X)
label1 = tk.Label(root,text="Insert the first side dimension").pack(fill=X,ipady=10)
entrybox1 = tk.Entry(root,font= ("Arial 12"),textvariable=v1).pack(fill=X,ipady=7)
label2 = tk.Label(root,text="Insert the second side dimension").pack(fill=X,ipady=10)
entrybox2 = tk.Entry(root,font=("Arial 12"),textvariable=v2).pack(fill=X,ipady=7)
button = tk.Button(root,font=("Arial 12"), command= showmsg,text="Done",state=st).pack(fill= "x")
root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • How are you running this script? does the program get stuck in an infinite loop, or terminate immediately? what do you mean by "not run"? – Freddy Mcloughlan Jun 06 '22 at 00:02
  • 1
    Im running it in VSCODE,in the terminal, seems like it's stuck into a infinite and "clear" loop(without anything inside), because nothing is being shown, I don't think it crashed because you can change the code and VSCODE works properly, only the terminal gets blank – Nome Sobrenome Jun 06 '22 at 00:23
  • 1
    The first while loop is an infinite loop. Don't use while loop in the main thread of a tkinter application. Use `.after()` to replace the two while loops or think clearly whether the two while loops are necessary. – acw1668 Jun 06 '22 at 00:42
  • `tkinter` programs are even-driven, so you can't write procedural code like you are when using it. There are quite a few other errors in your code related to it use, plus it appears you don't fully understand how classes work. I suggest you first learn Python better then find a good `tkinter` tutorial somewhere, and only then make another attempt at this writing program. – martineau Jun 06 '22 at 01:26
  • I see, this first class was made for no reason at all and I mixed object-oriented and procedural, right? – Nome Sobrenome Jun 06 '22 at 02:37
  • The issues with the class are unrelated to the use of procedure code — it's with how classes work. The problem is not with OOP vs Procedural programming, it's between [Event-driven programming](https://en.wikipedia.org/wiki/Event-driven_programming) and [Procedural programming](https://en.wikipedia.org/wiki/Procedural_programming). Also see Bryan Oakley's [answer](https://stackoverflow.com/a/9343402/355230) to the question [Tkinter — executing functions over time](https://stackoverflow.com/questions/9342757/tkinter-executing-functions-over-time). – martineau Jun 06 '22 at 13:43

1 Answers1

0

I have tried to edit your code with a few comment. Please note them:

from math import *
import tkinter as tk
from tkinter import *
from tkinter import ttk
root = tk.Tk()
#USER choosen sizes:
wwdimensions = 300
whdimensions = 300
#Class to get screen dimensions easier
class winfo:
    swidth = root.winfo_screenwidth()
    sheight = root.winfo_screenheight()
    def width(screenwidth,windowwidth):
        intendedposwidth = int(screenwidth/2 - windowwidth/2)
        return(intendedposwidth)
    def height(screenheight,windowheight):
        intendedposheight = int(screenheight/2 - windowheight/2)
        return(intendedposheight)
screenwidth=winfo.swidth
screenheight = winfo.sheight
inputcenterw=winfo.width(screenwidth,wwdimensions)
inputcenterh=winfo.height(screenheight,whdimensions)
root.geometry("{}x{}+{}+{}".format(wwdimensions,whdimensions,inputcenterw,inputcenterh))

root.title("Pythagoream Theorem")
v1 = tk.StringVar()
v2 = tk.StringVar()
val1 = v1.get()
val2 = v2.get()
st = "disabled"
while val1 or val2 == "":  # This line entered into an infinite loop because val2 is truly an empty string. There's no point for a while...loop here.
    st = "disabled"
    la=tk.Label(root,text="Fill the entry spaces").pack(fill=X,ipady=10,side= "bottom")
    break  # If you insist on using a while...loop, then use this break here.
else:
    st="disabled"

while val1 or val2 == str:  # Unecessary while...loop. val1 is Falsey & val2 is always False too. You created it and get from StringVar. It's always str but it's type(val2) == str that will evaluate to True.
    try:
        val2 = float(val2)
        val1 = float(val1)
    except:
        lab=tk.Label(root,text="Invalid dimensions,please solve it").pack(ipady=10,side="bottom",fill= X)
        st = "disabled"
    else:
        st = "normal"
st = "normal"
def showmsg():
    # NOTE: your v1 and v2 are your StringVar object not val1 and val2 which are just strings. I changed those to get the real user input for your computation.
    result = tk.Label(root, text = f"The hypotenuse is {sqrt(float(v1.get() or 0)**2+float(v2.get() or 0)**2)}",font=("Arial 12"))
    result.pack(ipady=10,side="bottom",fill=X)
label1 = tk.Label(root,text="Insert the first side dimension").pack(fill=X,ipady=10)
entrybox1 = tk.Entry(root,font= ("Arial 12"),textvariable=v1).pack(fill=X,ipady=7)
label2 = tk.Label(root,text="Insert the second side dimension").pack(fill=X,ipady=10)
entrybox2 = tk.Entry(root,font=("Arial 12"),textvariable=v2).pack(fill=X,ipady=7)
button = tk.Button(root,font=("Arial 12"), command= showmsg,text="Done",state=st).pack(fill= "x")
root.mainloop()

Meanwhile, consider not re-creating your result widget every time the "Done" button is pressed. Let me know if this help.

McKUNJOUS
  • 67
  • 4