2017-01-25 8 views
-1

그래서 몇 가지 답변을 읽었을 것입니다.하지만 대부분 중복되는 질문은 꽤 개인적이며 여기에 적용 할 수 있는지 여부는 모르겠습니다.)Tkinter 객체에는 'tk'속성이 없습니다

생각나 나는 꽤 큰이 예외를 받고 있어요 : (다음 퀴즈 클래스를 호출

#main.py 
    if self.state == self.states["Translate"]: 
     if self.translate is None: 
      self.translate = Translate.Translate(self, self.mainFrame) 
      print("translate frame is none") 
      print("did not switch") 
     else: 
      print("Switched to Quiz") 
      self.translate.translateFrame.destroy() 
      self.master.title("Quiz") 
      self.state = self.states["Quiz"] 
      self.quiz = Quiz.Quiz(self, self.mainFrame) 

모두 : 기본적으로

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1550, in __call__ 
    return self.func(*args) 
    File "/Users/acrobat/PycharmProjects/Jan9coderun/TranslatorVCS/main.py", line 85, in switchMode 
    self.quiz = Quiz.Quiz(self, self.mainFrame) 
    File "/Users/acrobat/PycharmProjects/Jan9coderun/TranslatorVCS/Quiz.py", line 84, in __init__ 
    self.makeQuiz() 
    File "/Users/acrobat/PycharmProjects/Jan9coderun/TranslatorVCS/Quiz.py", line 186, in makeQuiz 
    self.l = tk.Label(self, textvariable=self.watch.timestr) 
    File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 2606, in __init__ 
    Widget.__init__(self, master, 'label', cnf, kw) 
    File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 2132, in __init__ 
    BaseWidget._setup(self, master, cnf) 
    File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 2110, in _setup 
    self.tk = master.tk 
AttributeError: 'Quiz' object has no attribute 'tk' 

을 다른 파일에 같이 시작합니다 영형 는 다시

class Quiz: 
    def __init__(self, app, parent): 
     #self values here... 
     self.makeQuiz() 

내가 코드의 상단에 from stopWatch import StopWatch as Watch 로 수입되는 코드

self.watch = Watch(self.playLabel) 
self.l = tk.Label(self, textvariable=self.watch.timestr) 
self.watch.setTime(self.watch.elapsedtime) 

을의이 비트를 추가 할 때까지 전에 근무 makeQuiz 기능에 다음과 F이) 잘 작동 스톱워치는 다음과 같이 보입니다.

나는 여기에 다른 질문의 코드를 가지고 갔지만 아마 잘못 작성했을 것입니다. 별도의 창은 아니지만이 기능을 다른 Windows의 코드처럼 구현하여 현재 창에있는 내용을 대체하려고했습니다.

+0

'self.watch = Watch (self.playLabel)'에서 전달되는'parent' 인자가 루트 윈도우가 아닌'Label' 객체이거나' tk.Frame' 인스턴스가 있어야합니다. – martineau

답변

2

Quiz은 위젯이 아니고 객체입니다. 그러나 위젯처럼 사용하는 것 같습니다.

class Quiz: 
    def __init__(self, app, parent): 
     ... 
     self.makeQuiz() 
    def makeQuiz(): 
     ... 
     self.l = tk.Label(self, ...) 

그래서, 당신은 위젯에 부모 같은 비 위젯 (self)을 사용하고 있습니다 : 귀하의 질문에 코드를 조립할, 나는 부분에 클래스 정의는 다음과 같이 보입니다 있으리라 믿고있어. Tkinter는 부모가 tk 속성을 가지고 있다고 가정하지만, 분명히 Quiz 객체는 위젯이 아니기 때문에 그 속성을 가지고 있지 않습니다.

의도 한 바가 있거나 이미 만든 위젯이 무엇인지 모르겠지만이 특정 전화 번호 tk.Label 대신 self을 실제 위젯으로 바꿔야합니다.

+0

감사합니다. 나는 너와 똑같은 결론에 도달했으나, 내 와이파이가 조금씩 사라지기 때문에 너를 받아 들일 수 없었다. – CatsInSpace

-2

Tkinter/tkinter에는 "tk"속성이 없으며 "Tk"속성이 있습니다. 이것

테이크 모양 :

py2.7 :

>>>import Tkinter 
>>>"Tk" in dir(Tkinter) 
>>> True 

py3.5 : 당신은 그냥 Tk.Frame "와"tk.Frame을 "이름을 바꿀 필요가

>>>import tkinter 
>>> "Tk" in dir(tkinter) 
>>> True" 

"

+0

미안하지만, 너무 명확하지 않았다. (나는 언급하지 않았다.) tkinter를 tk로 가져왔다. 왜냐하면 tk (tk와 tkinter) – CatsInSpace

관련 문제