我遇到了在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()
以下传递变量的方式给了我一个keyerror。任何帮助表示赞赏。
答案 0 :(得分: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
答案 1 :(得分: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()
你的代码中至少有一些错误......它至少使框架现在