0

Shown below is the code for a tkinter login window, I'm trying to get a logo to display at the top of the screen, but get this error when doing so:

NameError: global name 'tkinter' is not defined

Here is my full code:

*original code was here

Thanks

CODE AFTER EDIT (Removing tkinter.):

from tkinter import *

root = Tk()

root.title("Speed Wars Login")

def createInterface():
    Label(root, text="Log in").grid(row=1, column=0)
    Label(root, text="Create Account").grid(row=1, column=3)
    #PLACE IMAGE
    canvas = Canvas(root)
    canvas.grid(row=0, column=2)
    photo = PhotoImage(file = "logo.gif")
    canvas.create_image(0, 0, image=photo)

    Label(root, text="Username").grid(row=2, column=0)
    Label(root, text="Password").grid(row=3, column=0)
    global usrnm
    global psswrd
    usrnm = Entry(root, width = 15)
    psswrd = Entry(root, show="*", width = 15)

    usrnm.grid(row=2, column=1)
    psswrd.grid(row=3, column=1)

def loginprint():
    print("Username: %s\nPassword: %s" % (usrnm.get(), psswrd.get()))

createInterface()

Button(root, text="Login", command = loginprint).grid(row=3, column=0)

root.mainloop()
RossC
  • 101
  • 2
  • 8
  • 1
    You have imported everything from `tkinter` by using `*`. Therefore you don't need to do `tkinter.something`. FYI it's not good practice to use `from module import *`. – DavidG Oct 12 '17 at 08:45
  • I'll try that, thanks! – RossC Oct 12 '17 at 08:47
  • 1
    See [this](https://stackoverflow.com/questions/710551/import-module-or-from-module-import) question/answer for the different ways to import a module and their pros/cons – DavidG Oct 12 '17 at 08:52
  • 1
    What DavidG & the question he linked said. "Star" imports make code harder to read. And they dump all of those names into your namespace. FWIW, a Tkinter star import brings in 136 names in Python 3, and 175 names in Python 2. – PM 2Ring Oct 12 '17 at 09:25

1 Answers1

2

It's because you are telling tkinter to import as *, which imports everything into the namespace regardless of its name.

Lose the tkinter. at the beginning of creating instances like PhotoImage and everything will work.

However, I would strongly advise that you instead change the import to import tkinter as tk, that way you can specify tk.Label, tk.PhotoImage, etc, etc and you won't have to worry about object names conflicting or having to write out the full tkinter before each Object used.

Benjamin James Drury
  • 2,353
  • 1
  • 14
  • 27
  • Thanks for promoting the use of `import tkinter as tk`. – PM 2Ring Oct 12 '17 at 09:24
  • The window does open now, however, the image is not displayed, just a blank space where it would be, see my edited code above. – RossC Oct 12 '17 at 10:48
  • That's a separate issue, if you can't figure it out, then you should open up another question. That said, the first thing I notice is that `photo = PhotoImage(file = "logo.gif")` will be garbage collected, so change that line to `canvas.photo = PhotoImage(file = "logo.gif")` and the following to `canvas.create_image(0, 0, image=canvas.photo)` – Benjamin James Drury Oct 12 '17 at 10:56