Home > Blockchain >  I keep getting NameError: name 'root' is not defined in Tkinter when running a registratio
I keep getting NameError: name 'root' is not defined in Tkinter when running a registratio

Time:12-27

I keep getting : NameError: name 'root' is not defined when running the code:

    import sqlite3
from tkinter import *

def register():
    root = Tk()
    root.geometry("300x200")
    root.title("Register App 1.0")
    root.resizable(False, False)

    myLabelEntry = Label(root, text="Register Project", font=("Arial", 25), bg = "grey", width = "300")
    myLabelEntry.pack()

    beginLabel = Label(root, text="To Begin Registration, Click Y:", font=("Arial", 15))
    beginLabel.pack()

    beginButton = Button(root, text="Y", font=("Arial", 15))
    beginButton.pack()

register()


root.mainloop()

I have ran code similar to this before which seemed to work however that didn't involve many functions if any at all. Any help with this would be much appreciated

CodePudding user response:

root is a variable created inside the register() function, so it can't use it outside of it. To solve it, just create the window outside the function or make a variable global.

global root

or

from tkinter import *
root = Tk()
root.geometry("300x200")
root.title("Register App 1.0")
root.resizable(False, False)
def register():
    #program
register()
root.mainloop()
  • Related