简体   繁体   English

使用Tkinter的图形GUI抛出'NameError'

[英]Drawing GUI using Tkinter throws 'NameError'

I'm trying to improve my programming skills, and have been doing simple Python projects. 我正在尝试提高编程技能,并且一直在做简单的Python项目。 I decided to try my hand at a GUI program, and have been following this tutorial for guidance. 我决定尝试使用GUI程序,并一直按照本教程进行指导。

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. 我正在使用Python 3.7,并在Windows的IDLE中进行编辑。

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) 例子中有我没有的错误(例如未引用/分配Tk())或根本不适用(某人的全局Window变量不起作用,显然是因为线程在Tkinter中不起作用)

I'm very new to Python and generally inexperienced with programming, so any advice/guidance would be appreciated. 我是Python的新手,通常对编程没有经验,因此任何建议/指导都将不胜感激。

Python uses whitespace to define scope. Python使用空格定义范围。 You've made a class called Window, but your __init__() and init_window() functions exist outside of it. 您已经创建了一个名为Window的类,但是__init__()init_window()函数位于init_window()之外。

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: 要解决此问题,请确保两个类方法均正确缩进,以便它们属于Window类:

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. 以上为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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