Search code examples
pythonvariablestkintertkinter-entry

How do you use tkinter entry boxes to make a variable?


I'm currently trying to create an "orbit simulator" and this part of the code is part of it. However an errors occur when I try to run it. The get() function seems to not work as it simply outputs that it doesn't exist. I'm really stumped at this.

import tkinter
runwin = tkinter.Tk()
runwin.title("Orbit Sim")
runwin.geometry("320x320")
def run21():
    dt=ent21.get("1.0")
    tg=ent22.get("1.0")
    xz=ent23.get("1.0")
    yz=ent24.get("1.0")
    velz=ent25.get("1.0")
    runwin.destroy()
lbl21 = tkinter.Label(runwin, text="How long to simulate for?").pack()
ent21 = tkinter.Entry(runwin).pack()
lbl22 = tkinter.Label(runwin, text="How many seconds pass per check?").pack()
ent22 = tkinter.Entry(runwin).pack()
lbl23 = tkinter.Label(runwin, text="Starting Positon? Please state X then Y.").pack()
ent23 = tkinter.Entry(runwin).pack()
ent24 = tkinter.Entry(runwin).pack()
lbl24 = tkinter.Label(runwin, text="Starting Velocity").pack()
ent25 = tkinter.Entry(runwin).pack()
btn21 = tkinter.Button(runwin, text="Submit", command=run21).pack()
runwin.mainloop()
t=0
while t < dt:
    r3, t =m.sqrt((xz*xz)+(yz*yz)), t+tg

P.S. I'm not a genius at coding and the way i've written this code is pretty much the only way I can understand it without sufficient notes.


Solution

  • You have 3 problems I can see.

    1st problem is you are using the grid manager on the widget directly and this will cause your get() method to error out. That is be cause the grid manager is returning None. We can fix this by calling the grid manager on a new line.

    2nd you are putting "1.0" in the get method and this is going to error out. Just leave it blank like this get().

    3rd you need to define your variables to be ran after the program closes as outside of the tkinter instance. Then you need to set the global call up in your function.

    Take a look at the below code:

    import tkinter
    # the 5 below variables are listed outside of tkinter so your while statement
    # after the mainloop can use the data.
    dt = ""
    tg = ""
    xz = ""
    yz = ""
    velz = ""
    
    runwin = tkinter.Tk()
    runwin.title("Orbit Sim")
    runwin.geometry("320x320")
    def run21():
        # this global is needed to tell the run21 function the variables are
        # in the global namespace.
        global dt, tg, xz, yz, velz
        dt=ent21.get()
        tg=ent22.get()
        xz=ent23.get()
        yz=ent24.get()
        velz=ent25.get()
        runwin.destroy()
    lbl21 = tkinter.Label(runwin, text="How long to simulate for?").pack()
    ent21 = tkinter.Entry(runwin)
    ent21.pack()
    lbl22 = tkinter.Label(runwin, text="How many seconds pass per check?").pack()
    ent22 = tkinter.Entry(runwin)
    ent22.pack()
    lbl23 = tkinter.Label(runwin, text="Starting Positon? Please state X then Y.").pack()
    ent23 = tkinter.Entry(runwin)
    ent23.pack()
    ent24 = tkinter.Entry(runwin)
    ent24.pack()
    lbl24 = tkinter.Label(runwin, text="Starting Velocity").pack()
    ent25 = tkinter.Entry(runwin)
    ent25.pack()
    btn21 = tkinter.Button(runwin, text="Submit", command=run21).pack()
    t=0
    runwin.mainloop()
    
    print(dt, tg, xz, yz, velz)
    # commented out as it is not testable without knowing what "m" is.
    # while t < dt:
    #     r3, t = m.sqrt((xz*xz)+(yz*yz)), t+tg