0

I have made 2 py files 'Main' and GUIHelperFunctions in the same folder and I want to use the function quitprogram from that module in my main file, the exact same way as if I just made the function in the main file itself. but I am getting an

1importError: Cannot import name 'quitprogram' from GUIhelperFunction1.

How do I solve it?

My code in GUIHelperFunctions.py:

    def quitProgram():
        root.quit()

and in my 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()

Edit 1:

I tried to solve it some more and I return to this error even then I make sure that it is written as my post does it. If I put the function from GUIHelperFunction into my main file, the program freezes.

    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

Edit 2:

Okay the freeze was due to using root.quit() instead of root.destroy(). The above problem is still actual though.

Muhammad Usman Bashir
  • 1,441
  • 2
  • 14
  • 43
  • probably a typo. `GUIhelperFunction` or `GUIHelperFunction` or `GUIhelperFunctions` or `GUIHelperFunctions`? – hongsy Jan 21 '20 at 04:08
  • No it's not a typo but thanks. The only typo was what I wrote as my comment. Sorry – Christopher Sonne Jan 21 '20 at 04:31
  • you might have cyclical dependencies. can you paste the imports in `GUIHelperFunctions.py`? – hongsy Jan 21 '20 at 05:26
  • There is none, I now get another error after I restarted the IDE. 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. Before that It just freezed then I clicked the button. So I'm cycling between these 3 issues with just deleting and writing the same things again. – Christopher Sonne Jan 21 '20 at 05:35
  • The only code at the `GUIHelperFunctions` is the one you added? How can it access the `root` variable if it does not defined or passed to it? – Amiram Jan 21 '20 at 06:52

1 Answers1

0

According to the exception your problem is not with the import of the quitProgram function it with the function itself.

NameError: name 'root' is not defined

When you import the function from the different file it does not aware to all the main file variables i.e (in the context of your code) the root variable is not defined at the GUIHelperFunctions therefor it is not recognized at the quitProgram function therefor it can't be used there -> results with the NameError exception.

I would suggest the following changes so it would work:

  1. Change the signature of the quitProgram function:
def quitProgram(root):
    root.destroy()
  1. Call the function with the relevant parameter:
quit = tk.Button(root, text="quit", bg='#AFAFAF', command=lambda: quitProgram(root))

Please see How to pass arguments to a Button command in Tkinter? for more information and examples.

Amiram
  • 1,227
  • 6
  • 14