2014-04-19 3 views
0

코드입니다 :파이썬 나가서 설명하자면 NameError는 : 이름 '프레임'정의되지 않은 (Tkinter를)은 여기

#!/usr/bin/python 
from tkinter import * 

class App: 
    def _init_(self, master): 

     frame = Frame(master) 
     frame.pack() 

    self.lbl = Label(frame, text = "Hello World!\n") 
    self.lbl.pack() 

    self.button = Button(frame, text="Quit", fg="red", command=frame.quit) 
    self.button.pack(side=LEFT) 

    self.hi_there = Button(frame, text="Say hi!", command=self.say_hi) 
    self.hi_there.pack(side=LEFT) 

    def say_hi(self): 
     print("Hello!") 

    root = Tk() 
    root.title("Hai") 
    root.geometry("200x85") 
    app = App(root) 
    root.mainloop() 

그리고 여기, 오류 : 잘못 어디로 갔는지

Traceback (most recent call last): 
    File "F:/HTML/HTMLtests/python/hellotkinter2.py", line 4, in <module> 
    class App: 
    File "F:/HTML/HTMLtests/python/hellotkinter2.py", line 10, in App 
    self.lbl = Label(frame, text = "Hello World!\n") 
NameError: name 'frame' is not defined 

은 찾을 수 없습니다! 어떤 도움을 주셔서 감사합니다!

답변

4

잘못 여기 꽤있다 :

  1. 그것은 __init__하지 _init_의가.
  2. 클래스 멤버 변수 (__init__으로 설정되지 않음)와 인스턴스 멤버 변수 (__init__으로 설정) 사이의 차이점을 알아야합니다. 완전히 잘못된 self을 사용 중입니다.
  3. 클래스가 반복적으로 인스턴스를 생성하는 것으로 보입니까 ??
  4. 전체 세방을하는 거대한 형태가없는 클래스 대신 우려를 분리해야합니다.

귀하의 오류가이 때문이다 그러나 당신이 1을보고 3

1

들여 쓰기와 대문자가 일부 밑줄/w 함께 꺼져있을 때까지 완전히 해결되지 않습니다. 다음 작품들.

#!/usr/bin/python 
from Tkinter import * 

class App(object): 
    def __init__(self, master): 
     frame = Frame(master) 
     frame.pack() 

     self.lbl = Label(frame, text = "Hello World!\n") 
     self.lbl.pack() 

     self.button = Button(frame, text="Quit", fg="red", command=frame.quit) 
     self.button.pack(side=LEFT) 

     self.hi_there = Button(frame, text="Say hi!", command=self.say_hi) 
     self.hi_there.pack(side=LEFT) 

    def say_hi(self): 
     print("Hello!") 

root = Tk() 
root.title("Hai") 
root.geometry("200x85") 
app = App(root) 
root.mainloop() 
관련 문제