1

wxpython で、あるクラスから別のクラスに textCtrl データを渡す際に問題があります。変数を渡すインスタンス メソッドを使用してみましたが、init _function を使用すると、プログラムの開始時にのみ関連し、最初の開始後のテキスト コントロール ボックスへの変更は考慮されません。Update() または Refresh() を試しましたが、どちらも機能しませんでした。

簡略化したコードを次に示します。

class DropTarget(wx.DropTarget):


     def __init__(self,textCtrl, *args, **kwargs):
          super(DropTarget, self).__init__( *args, **kwargs)
          self.tc2=kwargs["tc2"]
          print self.tc2


class Frame(wx.Frame):

     def __init__(self, parent, tc2):
     self.tc2 = wx.TextCtrl(self, -1, size=(100, -1),pos = (170,60))#part number

def main():

     ex = wx.App()
     frame = Frame(None, None)
     frame.Show()
     b = DropTarget(None, kwarg['tc2'])
     ex.MainLoop()

if __name__ == '__main__':
    main()

変数を渡す次の方法では、キーエラーが発生します。どんな助けでも大歓迎です。

4

2 に答える 2

1

これは問題に対する最もエレガントな解決策ではありませんが、同様の問題がありました。テキストを一時テキスト ファイルにダンプすると、いつでも元に戻すことができます。したがって、次のようになります。

tmpFile = open("temp.txt",'w')
tmpFile.write(self.tc2.GetValue())
tmpFile.close()

#when you want the string back in the next class
tmpFile = open("temp.txt",'r')
string = tmpFile.read()
tmpFile.close()
os.system("del temp.txt")    #This just removes the file to clean it up, you'll need to import os if you want to do this
于 2013-11-01T19:06:50.770 に答える