Python 3.x 导入错误:无法导入名称';退出计划';从';GUIhelperFunction';

Python 3.x 导入错误:无法导入名称';退出计划';从';GUIhelperFunction';,python-3.x,tk,Python 3.x,Tk,我在同一个文件夹中创建了2个py文件“Main”和GUIHelperFunctions,我想在Main文件中使用该模块中的函数quitprogram,就像我在Main文件中创建函数一样。但是我得到了一份工作 1导入错误:无法从GuiHelperFunction 1导入名称“quitprogram” 我如何解决它 GUIHelperFunctions.py中的我的代码: def quitProgram(): root.quit() 在我的main.py中: imp

我在同一个文件夹中创建了2个py文件“Main”和
GUIHelperFunctions
,我想在
Main
文件中使用该模块中的函数
quitprogram
,就像我在
Main
文件中创建函数一样。但是我得到了一份工作

1导入错误:无法从GuiHelperFunction 1导入名称“quitprogram”

我如何解决它

GUIHelperFunctions.py中的我的代码:

    def quitProgram():
        root.quit()
在我的main.py中:

    import numpy as np
    import beam as bm
    import matplotlib.pyplot as plt

    from GUIHelperFunctions import quitProgram
    import tkinter as tk  # tkinter is a GUI implementation Library

    HEIGHT = 480
    WIDTH = 640

    root = tk.TK()

    canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
    canvas.pack()

    # Creates a frame where GUI elements are drawn
    frame = tk.Frame(root)
    frame.place(relwidth=1, relheight=0.95)

    # Creating variables that are put into the frame GUI (start up)
    myLabel = tk.Label(frame, text="Deflection of beam Calculator", bg='yellow')


    quit = tk.Button(root, text="quit", bg='#AFAFAF', command=quitProgram)

    myLabel.pack()
    quit.pack()

    root.mainloop()
编辑1:

我试图进一步解决这个问题,然后返回到这个
错误
,即使这样,我也确保它是按照我的帖子所做的那样编写的。如果我将
GUIHelperFunction
中的函数放入我的
main
文件,程序将冻结

    Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Users\chris\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
        return self.func(*args)
      File "C:\Users\chris\OneDrive\Desktop\code Assignments\Exam Project\GUIHelperFunctions.py", line 8, in quitProgram
    NameError: name 'root' is not defined
编辑2:


好的,冻结是因为使用了
root.quit()
而不是
root.destroy()
。但上述问题仍然存在。

根据例外情况,您的问题不在于
退出程序
函数的
导入
,而在于函数本身

NameError:未定义名称“根”

当您从不同的文件导入函数时,它不知道所有的主文件变量,即(在代码的上下文中)在
GUIHelperFunctions
中未定义
root
变量,因此在
quitProgram
函数中无法识别该变量,因此无法在那里使用它->导致
namererror
异常

我建议进行以下更改,使其生效:

  • 更改退出程序功能的签名:
  • 使用相关参数调用函数:

  • 有关更多信息和示例,请参见。

    可能是打字错误
    GUIhelperFunction
    GUIhelperFunction
    GUIhelperFunctions
    GUIhelperFunctions
    ?不,这不是打字错误,谢谢。唯一的错误是我写的评论。抱歉,您可能有周期性依赖关系。能否将导入粘贴到
    GUIHelperFunctions.py
    中?没有,我现在在重新启动IDE后遇到另一个错误。Tkinter回调回溯异常(最近一次调用上次):文件“C:\Users\chris\AppData\Local\Programs\Python\Python37-32\lib\Tkinter\u init\uuu.py”,第1705行,在调用返回self.func(*args)文件“C:\Users\chris\OneDrive\Desktop\code Assignments\example Project\GUIHelperFunctions.py”第8行,在quitProgram NameError中:未定义名称“root”。在此之前,它只是冻结,然后我点击按钮。因此,我在这三个问题之间循环,只是删除并再次写入相同的内容。在
    GUIHelperFunctions
    中唯一的代码是您添加的代码?如果未定义或传递给
    root
    变量,它如何访问该变量?通过使用您的更改,我得到类型错误:quitProgram()接受0个位置参数,但给出了1是的,我确实按照指定将“root”作为参数放入了GuiHelperFunction中的函数中,以及“lambda:quitProgram(root))中它在另一个IDE中工作,然后我改为写command=lambda:GUIHelperFunctions.quitProgram(root)。那就解决了,谢谢!
    def quitProgram(root):
        root.destroy()
    
    quit = tk.Button(root, text="quit", bg='#AFAFAF', command=lambda: quitProgram(root))