1

ユーザーに表示するには、メッセージをエラー ダイアログに渡す必要があります。

最初の試み

PreferencesDialog.py

dlgError = ErrorDialog ('Have you been messing with the configuration files?')
dlgError.run ()

ErrorDialog.py

global message = None

def __new__(cls, msg):
    builder = get_builder('ErrorDialog')
    new_object = builder.get_object('error_dialog')
    new_object.finish_initializing(builder)

    global message
    message = msg

    return new_object

def finish_initializing(self, builder):

    # Get a reference to the builder and set up the signals.
    self.builder = builder
    self.ui = builder.get_ui(self)

    global message
    self.builder.get_object ('lblMessage').set_text (message)

2 回目の試行

PreferencesDialog.py

dlgError = ErrorDialog ()
dlgError.message = 'Have you been messing with the configuration files?'
dlgError.run ()

ErrorDialog.py

global message = None

def __new__(cls):
    builder = get_builder('ErrorDialog')
    new_object = builder.get_object('error_dialog')
    new_object.finish_initializing(builder)

    return new_object

def finish_initializing(self, builder):

    # Get a reference to the builder and set up the signals.
    self.builder = builder
    self.ui = builder.get_ui(self)

    global message
    self.builder.get_object ('lblMessage').set_text (message)

どちらも同じ例外をスローします。

self.builder.get_object ('lblMessage').set_text (message)
NameError: global name 'message' is not defined

手伝ってください。Quickly、GTK 3、Python 2.7 を使用しています。

4

2 に答える 2

1

messageに設定して、をクラスインスタンスのメンバーにしてみません__init__か?

def __init__(self, msg):
  self._message = msg

def finish_initializing(self, builder):
  self.builder.get_object("lblMessage").set_text(self._message)
于 2012-06-27T11:25:35.110 に答える
0

ダイアログに変数を渡してテキストを入力ボックスに入れるダイアログをセットアップしました。トリックは、変数をnewに渡し、 finish.initializing() が呼び出されたときに変数をそこに入れることです。コードは次のとおりです。

class QuerynameDialog(Gtk.Dialog):
    __gtype_name__ = "QuerynameDialog"

    def __new__(cls, name):
        """Special static method that's automatically called by Python when 
        constructing a new instance of this class.

        Returns a fully instantiated QuerynameDialog object.
        """
        builder = get_builder('QuerynameDialog')
        new_object = builder.get_object('queryname_dialog')
        new_object.finish_initializing(builder,name)
        return new_object


    def finish_initializing(self, builder,name):
        """Called when we're finished initializing.

        finish_initalizing should be called after parsing the ui definition
        and creating a QuerynameDialog object with it in order to
        finish initializing the start of the new QuerynameDialog
        instance.
        """
        # Get a reference to the builder and set up the signals.
        self.builder = builder
        self.ui = builder.get_ui(self)
        self.builder.get_object('name_entry').set_text(name)
于 2012-07-22T13:58:48.413 に答える