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.
我正在尝试创建一个“轨道模拟器”,这部分代码是它的一部分。但是,当我尝试运行它时会发生错误。 get()函数似乎不起作用,因为它只是输出它不存在。我真的很难过。
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.
附:我不是编码的天才,我编写这段代码的方式几乎是我能够理解它而没有足够注释的唯一方法。
2 个解决方案
#1
0
You have 3 problems I can see.
你有3个问题我可以看到。
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.
第一个问题是您直接在窗口小部件上使用网格管理器,这将导致您的get()方法出错。这是因为网格管理器返回None。我们可以通过在新行上调用网格管理器来解决这个问题。
2nd you are putting "1.0"
in the get method and this is going to error out. Just leave it blank like this get()
.
第二,你在get方法中放了“1.0”,这就错了。把它留空就像这样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.
第三,你需要定义你的变量,以便在程序关闭后运行在tkinter实例之外。然后,您需要在函数中设置全局调用。
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
#2
0
Don't define a widget and use the layout manager on the same line if you wish to use the Widget.
如果您希望使用Widget,请不要定义窗口小部件并在同一行使用布局管理器。
i.e DON'T DO THIS
即不要这样做
ent21 = tkinter.Entry(runwin).pack()
DO THIS
ent21 = tkinter.Entry(runwin)
ent21.pack()
Like so
import tkinter
runwin = tkinter.Tk()
runwin.title("Orbit Sim")
runwin.geometry("320x320")
dt = ""
tg = ""
xz = ""
yz = ""
velz = ""
def run21():
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()
runwin.mainloop()
One of the other commenters is correct too. Any code after .mainloop
will not run until after the window is closed. Consider doing this inside the run21
function so it happens when the button is pressed.
其他评论者之一也是正确的。 .mainloop之后的任何代码都将在窗口关闭之后才会运行。考虑在run21函数中执行此操作,以便在按下按钮时发生这种情况。
I've removed "1.0"
from your get since the get method of an entry widget doesn't take any arguments.
我从你的get中删除了“1.0”,因为条目小部件的get方法不带任何参数。
Also please consider better naming for your variables. Instead of ent21
consider entrySimulationTime
or instead of ent24
consider entryStartingPosY
. Your code will be much easier to understand that way.
另外,请考虑更好地命名变量。而不是ent21考虑entrySimulationTime或而不是ent24考虑entryStartingPosY。您的代码将更容易理解。
EDIT: Added globals to expand the scope of dt etc.
编辑:添加全局变量以扩大dt的范围等。
#1
0
You have 3 problems I can see.
你有3个问题我可以看到。
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.
第一个问题是您直接在窗口小部件上使用网格管理器,这将导致您的get()方法出错。这是因为网格管理器返回None。我们可以通过在新行上调用网格管理器来解决这个问题。
2nd you are putting "1.0"
in the get method and this is going to error out. Just leave it blank like this get()
.
第二,你在get方法中放了“1.0”,这就错了。把它留空就像这样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.
第三,你需要定义你的变量,以便在程序关闭后运行在tkinter实例之外。然后,您需要在函数中设置全局调用。
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
#2
0
Don't define a widget and use the layout manager on the same line if you wish to use the Widget.
如果您希望使用Widget,请不要定义窗口小部件并在同一行使用布局管理器。
i.e DON'T DO THIS
即不要这样做
ent21 = tkinter.Entry(runwin).pack()
DO THIS
ent21 = tkinter.Entry(runwin)
ent21.pack()
Like so
import tkinter
runwin = tkinter.Tk()
runwin.title("Orbit Sim")
runwin.geometry("320x320")
dt = ""
tg = ""
xz = ""
yz = ""
velz = ""
def run21():
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()
runwin.mainloop()
One of the other commenters is correct too. Any code after .mainloop
will not run until after the window is closed. Consider doing this inside the run21
function so it happens when the button is pressed.
其他评论者之一也是正确的。 .mainloop之后的任何代码都将在窗口关闭之后才会运行。考虑在run21函数中执行此操作,以便在按下按钮时发生这种情况。
I've removed "1.0"
from your get since the get method of an entry widget doesn't take any arguments.
我从你的get中删除了“1.0”,因为条目小部件的get方法不带任何参数。
Also please consider better naming for your variables. Instead of ent21
consider entrySimulationTime
or instead of ent24
consider entryStartingPosY
. Your code will be much easier to understand that way.
另外,请考虑更好地命名变量。而不是ent21考虑entrySimulationTime或而不是ent24考虑entryStartingPosY。您的代码将更容易理解。
EDIT: Added globals to expand the scope of dt etc.
编辑:添加全局变量以扩大dt的范围等。