基本上,问题是这不起作用:
def run():
print song.get()
def startGUI():
root = Tk()
songLabel = Label(root, text="Enter the song:")
song = Entry(root)
submit = Button(root, text="Download", command = run)
songLabel.pack()
song.pack()
submit.pack()
root.mainloop()
if __name__ == "__main__":
startGUI()
鉴于此:
def run():
print song.get()
root = Tk()
songLabel = Label(root, text="Enter the song:")
song = Entry(root)
submit = Button(root, text="Download", command = run)
songLabel.pack()
song.pack()
submit.pack()
root.mainloop()
为什么我不能在方法中放置一个条目而不出错? 这里的具体错误是在run方法中找不到“song”,导致以下错误:
名称错误:未定义全局名称“song”
如何更改它以避免发生此错误,但条目仍在方法中?你知道吗
第一个代码中的
song
是局部变量,它只能在startGUI
函数中访问,在run
中不能访问。你知道吗song
在第二个代码中是全局变量,可以访问模块中的任何地方。你知道吗下面的代码展示了使第一个代码工作的一种方法。(传歌直奔)。你知道吗
另一种方法(在startGUI中运行):
你也可以用class。你知道吗
相关问题 更多 >
编程相关推荐