2013-04-29 162 views
1

我在将wxpython中的textCtrl数据从一个类传递到另一个类时存在问题。我尝试使用传递变量的实例方法,但如果我使用init _function,它只与程序开始时相关,并且不考虑初始启动后对文本控制框的任何更改。尝试了更新()或刷新(),它也没有工作。在wxpython中将变量从一个类传递到另一个类

这里是代码简化。

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() 

以下传递变量的方法给了我一个错误。任何帮助表示赞赏。

回答

0
import wx 
class DropTarget(wx.DropTarget): 


    def __init__(self,textCtrl, *args, **kwargs): 
      self.tc2 = kwargs.pop('tc2',None) #remove tc2 as it is not a valid keyword for wx.DropTarget 
      super(DropTarget, self).__init__(*args, **kwargs) 

      print self.tc2 


class Frame(wx.Frame): 

    def __init__(self, parent, tc2): 
     #initialize the frame 
     super(Frame,self).__init__(None,-1,"some title") 
     self.tc2 = wx.TextCtrl(self, -1, size=(100, -1),pos = (170,60))#part number 

def main(): 

    ex = wx.App(redirect=False) 
    frame = Frame(None, None) 
    frame.Show() 
    #this is how you pass a keyword argument 
    b = DropTarget(frame,tc2="something") 
    ex.MainLoop() 

if __name__ == '__main__': 
    main() 

有在你的代码至少有几个错误...它至少使框架现在

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 
相关问题