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.