YSR
YSR

Reputation: 69

NameError: name 'root' is not defined in python 3

Below is the program I am trying to run

from tkinter import *
import tkinter as tk
from tkinter.ttk import *
from tkinter import LabelFrame, Label, Tk#, Canvas
from tkinter.ttk import Notebook
import tkinter.messagebox
import time
import os


import warnings
warnings.filterwarnings('ignore')

class GUIDesign():
    def __init__(self,root):
        self.initUI(root) 

    def initUI(self,root):
        print ("hello")

    LabelFrameFG="purple"
    LabelFrameBG="SNOW"

    note = Notebook(root)
    tab1 = Frame(note)
    tab6 = Frame(note)

def main():
    root = Tk()
    root.resizable(0,0)
    root.state('zoomed')
    GUI=GUIDesign(root)
    root.mainloop()

if __name__ == '__main__':
    main()

it gives error:

Traceback (most recent call last):
File "E:\Python\tk.py", line 16, in <module>
class GUIDesign():
File "E:\Python\tk.py", line 35, in GUIDesign
note = Notebook(root)
NameError: name 'root' is not defined

The problem is in importing Notebook? or the class and its function? or python's version problem? Can anybody tell me what is wrong with this program please?

Upvotes: 0

Views: 15443

Answers (1)

Rajiv Makwana
Rajiv Makwana

Reputation: 34

You should define root as the global variable. Your code should be like this.

from tkinter import *
import tkinter as tk
from tkinter.ttk import *
from tkinter import LabelFrame, Label, Tk#, Canvas
from tkinter.ttk import Notebook
import tkinter.messagebox
import time
import os


import warnings
warnings.filterwarnings('ignore')

root = Tk()

class GUIDesign():
    def __init__(self,root):
        self.initUI(root) 

    def initUI(self,root):
        print ("hello")

    LabelFrameFG="purple"
    LabelFrameBG="SNOW"

    note = Notebook(root)
    tab1 = Frame(note)
    tab6 = Frame(note)


def main():
    root = Tk()
    root.resizable(0,0)
    root.state('zoomed')
    GUI=GUIDesign(root)
    root.mainloop()

if __name__ == '__main__':
    main()

Upvotes: 1

Related Questions