简体   繁体   中英

Drawing GUI using Tkinter throws 'NameError'

I'm trying to improve my programming skills, and have been doing simple Python projects. I decided to try my hand at a GUI program, and have been following this tutorial for guidance.

I'm not very far into it, the link takes you to the third section of the tutorial, and I'm running into an issue. I've copied the tutorial code almost exactly, just changing some strings to closer suit my end goal. (see my code below)

from tkinter import *

class Window(Frame):


def __init__(self, master = None):
    Frame.__init__(self, master)
    self.master = master
    self.init_window()

# Creation of init_window
def init_window(self):

    #changing the title of our master widget
    self.master.title("Path Browser")

    #allowing the widget to take the full space of the root window
    self.pack(fill = BOTH, expand = 1)

    #creating a button instance
    browseButton = Button(self, text = "Browse")

    #placeing the button on my window
    browseButton.place(x=0, y=0)

root = Tk()

#size the window
root.geometry("400x300")

app = Window(root)
root.mainloop()

When I run that code, it generates a window, but does not include any button. The shell outputs the following error message:

Traceback (most recent call last):

File "C:/Users/mmiller3/Python/GUITest.py", line 3, in <module>
    class Window(Frame):
  File "C:/Users/mmiller3/Python/GUITest.py", line 31, in Window
    app = Window(root)
NameError: name 'Window' is not defined

I'm using Python 3.7, and editing in IDLE for Windows.

I've googled around, and nothing I've found has proved helpful. Either the examples had mistakes that I didn't have (such as Tk() not being referenced/assigned) or simply weren't applicable (someone's global Window variable wasn't working, apparently because threading doesn't work in Tkinter)

I'm very new to Python and generally inexperienced with programming, so any advice/guidance would be appreciated.

Python uses whitespace to define scope. You've made a class called Window, but your __init__() and init_window() functions exist outside of it.

When you try to create a new Window in:

app = Window(root)

You haven't correctly instantiated the class as you intend.

To fix this, make sure both of your class methods are indented correctly so that they belong to the Window class:

from tkinter import *

class Window(Frame):


    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.init_window()

    # Creation of init_window
    def init_window(self):

        #changing the title of our master widget
        self.master.title("Path Browser")

        #allowing the widget to take the full space of the root window
        self.pack(fill = BOTH, expand = 1)

        #creating a button instance
        browseButton = Button(self, text = "Browse")

        #placeing the button on my window
        browseButton.place(x=0, y=0)

root = Tk()

#size the window
root.geometry("400x300")

app = Window(root)
root.mainloop()

The above worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM