[英]Calling self.variable from another class
How can I call variable from another class, I got 2 classes one is independent from another, but if something happens in first class I need reach label (self.lbl) in gui class and change it's value. 我怎么能从另一个类中调用变量,我得到了两个彼此独立的类,但是如果在第一类中发生了某些事情,我需要在gui类中到达标签(self.lbl)并更改其值。
class MyCalendar(wx.Frame):
def __init__(self, *args, **kargs):
wx.Frame.__init__(self, *args, **kargs)
self.cal = CalendarCtrl(self, 10, wx.DateTime.Now(),style= CAL_MONDAY_FIRST)
self.cal.Bind(wx.adv.EVT_CALENDAR, self.OnDate)
def OnDate(self,event):
if dateStatus:
global beginDate
tmp = datetime.datetime.strptime(str(self.cal.GetDate()), '%a %b %d %X %Y').strftime('%Y-%m-%d')
beginDate = datetime.datetime.strptime(tmp, '%Y-%m-%d').date()
#DataList.renewlabels()
else:
global endDate
tmp = datetime.datetime.strptime(str(self.cal.GetDate()), '%a %b %d %X %Y').strftime('%Y-%m-%d')
endDate = datetime.datetime.strptime(tmp, '%Y-%m-%d').date()
#DataList.renewlabels()
wx.Window.Close(self)
class DataList(wx.Frame,):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(830, 440))
self.Maximize(True)
panel = wx.Panel(self, -1)
uber_sizer = wx.BoxSizer(wx.VERTICAL)
top_sizer = wx.BoxSizer(wx.HORIZONTAL)
right_sizer = wx.BoxSizer(wx.VERTICAL)
#Labels
self.beginlbl = wx.StaticText(panel ,-1,style = wx.ALIGN_CENTER)
font = wx.Font(18, wx.ROMAN, wx.ITALIC, wx.NORMAL)
self.beginlbl.SetLabel(str(beginDate))
self.beginlbl.SetFont(font)
self.endlbl = wx.StaticText(panel ,-1,style = wx.ALIGN_CENTER)
self.endlbl.SetLabel(str(endDate))
self.endlbl.SetFont(font)
callCalendar()
def callCalendar():
app = wx.App()
frame = MyCalendar(None)
frame.Show()
app.MainLoop()
app = wx.App()
dl = DataList(None, -1, 'Radar Report')
dl.Show()
app.MainLoop()
This is little bit of code to get idea what I have. 这只是一点点代码,以使我了解我所拥有的。
EDIT: I missed one method in DataList class, as you can see I call it in MyCakendar.Ondate 编辑:我错过了DataList类中的一种方法,如您所见,我在MyCakendar.Ondate中称它为
def renewlabels(self)
self.beginlbl.SetLabel(str(beginDate))
If I understood your needs correctly, you can pass the first class' instance as a parameter to the other class' method, where you can change it. 如果我正确理解了您的需求,则可以将第一个类的实例作为参数传递给另一个类的方法,您可以在其中进行更改。
Here is an example. 这是一个例子。
class Class1():
def __init__(self, a):
self.a = a
class Class2():
def change(self, instance):
instance.a = 4
first = Class1(1)
print first.a
second = Class2()
second.change(first)
print first.a
So you can read or change a value in another class. 因此,您可以读取或更改另一个类中的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.