2009-09-01 3 views
2

나는 클래스를 만들고 있지만 파이썬에서 네임 스페이스에 문제가있다.메서드 호출 후 네임 스페이스가 변경되는 이유는 무엇입니까?

아래 코드를 볼 수 있으며 대부분 작동하지만, guiFrame._stateMachine()을 호출 한 후 시간 모듈이 어떻게 든 더 이상 정의되지 않습니다.

_stateMachine()에 시간 모듈을 다시 가져온 경우 작동합니다. 그렇다면 시간 모듈을 머리말에 가져올 때 이름 공간에없는 이유는 무엇입니까?

내가 누락 된 항목이 있습니까?

오류 메시지 :

File "C:\Scripts\Python\GUI.py", line 106, in <module> 
    guiFrame._stateMachine() 
    File "C:\Scripts\Python\GUI.py", line 74, in _stateMachine 
    self.tempfile.write('%s cpuUMTS %s\n' % (time.asctime(time.localt 
f.load.cpuThreadsValue['10094'])) 
UnboundLocalError: local variable 'time' referenced before assignment 

코드 :

import os 
import cpu_load_internal 
import throughput_internal 
import matplotlib 
matplotlib.use('Agg') 
import matplotlib.pyplot as plt 

from Tkinter import * 
import tkMessageBox 
import time 
class GUIFramework(Frame): 
    """This is the GUI""" 

    def __init__(self,master=None): 
     """Initialize yourself""" 

     """Initialise the base class""" 
     Frame.__init__(self,master) 

     """Set the Window Title""" 
     self.master.title("Type Some Text") 

     """Display the main window 
     with a little bit of padding""" 
     self.grid(padx=10,pady=10) 
     self.CreateWidgets() 
     plt.figure(1) 

    def _setup_parsing(self): 
     self.load = cpu_load_internal.CPULoad('C:\Templogs') 
     self.throughput = throughput_internal.MACThroughput('C:\Templogs') 
     self.tempfile = open('output.txt','w') 
     self.state = 0   

    def _parsing(self): 
     self.load.read_lines() 
     self.throughput.read_lines() 
     self.cpuLoad.set(self.load.cpuThreadsValue['10094']) 
     self.macThroughput.set(self.throughput.macULThroughput)   

    def __change_state1(self): 
     self.state = 2 

    def __change_state3(self): 
     self.state = 3  

    def CreateWidgets(self): 
     """Create all the widgets that we need""" 

     """Create the Text""" 
     self.cpuLoad = StringVar() 
     self.lbText1 = Label(self, textvariable=self.cpuLoad) 
     self.lbText1.grid(row=0, column=0) 

     self.macThroughput = StringVar() 
     self.lbText2 = Label(self, textvariable=self.macThroughput) 
     self.lbText2.grid(row=0, column=1) 

     self.butStart = Button(self, text = 'Start', command = self.__change_state1) 
     self.butStart.grid(row=1, column=0) 

     self.butStop = Button(self, text = 'Stop', command = self.__change_state3) 
     self.butStop.grid(row=1, column=1) 

    def _stateMachine(self): 
     if (self.state == 2): 
      print self.throughput.macULUpdate 
      print self.load.cpuUpdate 

      if self.load.cpuUpdate: 
       self.load.cpuUpdate = 0 
       print 'cpuUMTS %s\n' % (self.load.cpuThreadsValue['10094']) 
       self.tempfile.write('%s cpuUMTS %s\n' % (time.asctime(time.localtime()), self.load.cpuThreadsValue['10094']))    

      if self.throughput.macULUpdate: 
       self.throughput.macULUpdate = 0 
       print 'macUL %s %s\n' % (self.throughput.macULThroughput, self.throughput.macULThroughputUnit) 
       self.tempfile.write('%s macUL %s %s\n' % (time.asctime(time.localtime()), self.throughput.macULThroughput, self.throughput.macULThroughputUnit)) 

     if (self.state == 3): 
      self.tempfile.seek(0) 
      plt.plot([1,2,3],[1,4,6]) 
      plt.savefig('test.png') 
      self.state == 0 
      while 1: 
       try: 
        line = (self.tempfile.next()) 
       except: 
        break 

       if 'cpuUMTS' in line: 
        line.split 
        time = 4 


if __name__ == "__main__": 
    guiFrame = GUIFramework() 
    print dir(guiFrame) 
    guiFrame._setup_parsing() 
    guiFrame.state = 2 
    while(1): 
     guiFrame._parsing() 
     guiFrame._stateMachine() 
     guiFrame.update() 
     time.sleep(0.1) 

답변

7

가 왜 time에 할당합니까? 로컬 변수로 사용할 수 없으면 모듈을 덮어 버릴 것입니다! 자세히 보면, _stateMachine에서 로컬 변수로 사용하기 때문에 할당하기 전에 time을 사용한다고 불평합니다.

time = 4 
+1

아차 .. (:..! 내가 조금 흥분 어떻게 든 오류와 혼동있어 문제가 물론 감사 –

+0

@Fry :이 답변이 경우 "동의 함"을 체크 표시를 클릭하십시오 귀하의 질문에. –

2

시간을 변수로 사용하는 것 같습니다. 무슨 일이 발생합니다

"C : 파이썬 \ GUI.py \ \ 스크립트"라인이 방법에서 변수 time에 assing하려고 74

2

:

time = 4 

따라서 컴파일러는 시간이 true가 아닌 로컬 변수 여야한다고 가정합니다. 그리고 이것이 time에 할당하기 전에 모듈을 사용하려고해도 모듈 time을 사용할 때이 오류가 발생하는 이유입니다.

관련 문제