2012-01-04 7 views
2

モジュールのインポート例外を処理する方法はどこにも解決策がありません。私は 'エンチャント'モジュールをインポートする必要がありますが、最初にインストールされているかどうかを確認する必要があります。それがインストールされていない場合、エラーメッセージを表示する必要があります。したがって、私がこれを行うと、メインクラスがまだ作成されていないため、QMessageBoxを表示する方法がありません。PyQt - モジュールのインポート例外を処理する方法

import sys 
import re 

from PyQt4.QtCore import * 
from PyQt4.QtGui import * 

try: 
    import enchant 
    dict_list = enchant.list_languages() 
    if "ru_RU" in dict_list: 
     self.dict = enchant.Dict("ru_RU") 
    else: 
     self.dict = enchant.Dict() 
except ImportError, inst: 
    #How do I graphically show an error message here if the class hasn't been set up yet? 

class Translit(QMainWindow): 
    def __init__(self, parent = None): 
     super(Translit, self).__init__(parent) 

私が行う場合は、この:

import sys 
import re 

from PyQt4.QtCore import * 
from PyQt4.QtGui import * 

class Translit(QMainWindow): 
    def __init__(self, parent = None): 
     super(Translit, self).__init__(parent) 

    try: 
     import enchant 
     dict_list = enchant.list_languages() 
     if "ru_RU" in dict_list: 
      self.dict = enchant.Dict("ru_RU") 
     else: 
      self.dict = enchant.Dict() 
    except ImportError, inst: 
     QMessageBox.warning(parent, "", "Error:\n%s seems to be installed\n\nSpell checking will be disabled" % (inst)) 

    self.change_dict() 

    def change_dict(self): 
     self.dict = enchant.Dict("en_US") 
     QMessageBox.about(parent,"","Spellcheck is set to " + self.dict.tag) 

は、インタプリタは文句を言い、 "NameError:グローバル名 'エンチャント' は定義されていません"。

モジュールインポートの例外メッセージを表示する方法、またはプログラム全体を通してそのモジュールを動作させる方法を教えてください。ありがとうございました。

ここで私は再利用しようとしている元のソースです:スペルチェックは、ユーザーとして、オプション機能である場合

from PyQt4.QtCore import * 
from PyQt4.QtGui import * 

try: 
    import enchant 
except ImportError: 
    enchant = None 

class Translit(QMainWindow): 
    def __init__(self, parent = None): 
     super(Translit, self).__init__(parent) 
     if enchant is not None: 
      dict_list = enchant.list_languages() 
      if "ru_RU" in dict_list: 
       self.dict = enchant.Dict("ru_RU") 
      else: 
       self.dict = enchant.Dict() 
      self.change_dict() 
     else: 
      self.dict = None 
      QMessageBox.warning(parent, "", 
       "Error: could not import the 'enchant' module\n\n" 
       "Spell checking will be disabled") 

    def change_dict(self): 
     if self.dict is not None: 
      self.dict = enchant.Dict("en_US") 
      QMessageBox.about(
       parent, "", "Spellcheck is set to " + self.dict.tag) 

、しかし:

__license__ = 'MIT' 
__copyright__ = '2009, John Schember ' 
__docformat__ = 'restructuredtext en' 

import re 
import sys 

import enchant 

from PyQt4.Qt import QAction 
from PyQt4.Qt import QApplication 
from PyQt4.Qt import QEvent 
from PyQt4.Qt import QMenu 
from PyQt4.Qt import QMouseEvent 
from PyQt4.Qt import QPlainTextEdit 
from PyQt4.Qt import QSyntaxHighlighter 
from PyQt4.Qt import QTextCharFormat 
from PyQt4.Qt import QTextCursor 
from PyQt4.Qt import Qt 
from PyQt4.QtCore import pyqtSignal 


class SpellTextEdit(QPlainTextEdit): 

def __init__(self, *args): 
    QPlainTextEdit.__init__(self, *args) 

    # Default dictionary based on the current locale. 
    self.dict = enchant.Dict("ru_RU") 
    self.highlighter = Highlighter(self.document()) 
    self.highlighter.setDict(self.dict) 

def mousePressEvent(self, event): 
    if event.button() == Qt.RightButton: 
     # Rewrite the mouse event to a left button event so the cursor is 
     # moved to the location of the pointer. 
     event = QMouseEvent(QEvent.MouseButtonPress, event.pos(), 
      Qt.LeftButton, Qt.LeftButton, Qt.NoModifier) 
    QPlainTextEdit.mousePressEvent(self, event) 

def contextMenuEvent(self, event): 
    popup_menu = self.createStandardContextMenu() 

    # Select the word under the cursor. 
    cursor = self.textCursor() 
    cursor.select(QTextCursor.WordUnderCursor) 
    self.setTextCursor(cursor) 

    # Check if the selected word is misspelled and offer spelling 
    # suggestions if it is. 
    if self.textCursor().hasSelection(): 
     text = unicode(self.textCursor().selectedText()) 
     if not self.dict.check(text): 
      spell_menu = QMenu('Spelling Suggestions') 
      for word in self.dict.suggest(text): 
       action = SpellAction(word, spell_menu) 
       action.correct.connect(self.correctWord) 
       spell_menu.addAction(action) 
      # Only add the spelling suggests to the menu if there are 
      # suggestions. 
      if len(spell_menu.actions()) != 0: 
       popup_menu.insertSeparator(popup_menu.actions()[0]) 
       popup_menu.insertMenu(popup_menu.actions()[0], spell_menu) 

    popup_menu.exec_(event.globalPos()) 

def correctWord(self, word): 
    ''' 
    Replaces the selected text with word. 
    ''' 
    cursor = self.textCursor() 
    cursor.beginEditBlock() 

    cursor.removeSelectedText() 
    cursor.insertText(word) 

    cursor.endEditBlock() 

class Highlighter(QSyntaxHighlighter): 

WORDS = u'(?iu)[\w\']+' 

def __init__(self, *args): 
    QSyntaxHighlighter.__init__(self, *args) 

    self.dict = None 

def setDict(self, dict): 
    self.dict = dict 

def highlightBlock(self, text): 
    if not self.dict: 
     return 

    text = unicode(text) 

    format = QTextCharFormat() 
    format.setUnderlineColor(Qt.red) 
    format.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline) 

    for word_object in re.finditer(self.WORDS, text): 
     if not self.dict.check(word_object.group()): 
      self.setFormat(word_object.start(), 
       word_object.end() - word_object.start(), format) 

class SpellAction(QAction): 

''' 
A special QAction that returns the text in a signal. 
''' 

correct = pyqtSignal(unicode) 

def __init__(self, *args): 
    QAction.__init__(self, *args) 

    self.triggered.connect(lambda x: self.correct.emit(
     unicode(self.text()))) 

def main(args=sys.argv): 
app = QApplication(args) 

spellEdit = SpellTextEdit() 
spellEdit.show() 

return app.exec_() 

if __name__ == '__main__': 
    sys.exit(main()) 
+0

あなたのアプリはそのモジュールなしで実行できますか?そうでない場合は、通常の例外でスクリプトを終了させます。 – Avaris

答えて

1

はここにあなたの問題のための1つの可能な修正はあります私がこのアプリケーションを走らせるたびにこの警告メッセージを受け取ったら、私はかなり迷惑になります。

ユーザーが最初にスペルチェッカーにアクセスしてからさらにアクセスを無効にすると、警告が表示されることをお勧めします。しかし、それをやり遂げる方法は、明らかにアプリケーション内の他の場所でenchantモジュールがどのように使用されているかによって決まります。

+0

ああ、私はその考えが好きです。しかし、問題はchange_dictが "NameError:global name 'エンチャント'が定義されていないということです。これを公開するとすぐに、魅力的なクラスの使い方を示したチュートリアルページがロードされます。実際には、希望の辞書を設定するためのブローカーがあります。 – linuxoid

+0

@ user665327インポートが失敗した場合、 'enchant'が' None'にグローバルに設定されているので、 'NameError'を取得してはいけません。 – ekhumoro

+0

ありがとうございました。しかし、別の愚かな質問。私はボタンを持っています、押されている時はスペルがオンです。しかし、もしそれが別のクラスによって行われたら、それを無効にすることはできますか?私は、クラスを破棄し、ボタンが再び押されたときに再びインスタンス化すると考えることができます。より良い解決策はありますか?ありがとうございました。 – linuxoid

関連する問題