I'm trying to code up an application to help me keep track of my students. Basically a customized notebook/gradebook. I hacked something together last summer that worked for this past year, but I need something better.
I'm going to pull each students record from a datab开发者_如何学编程ase, display it on my main page and have elements clickable to open a frame so that I can edit it. I need to pass information between these two frames and I'm an idiot because I can't seem to figure out how to alter the examples I've come across showing lambdas and same-class information passing.
On my main window I have a StaticText that looks like this
self.q1a_lbl = wx.StaticText(id=wxID_MAINWINDOWQ1A_LBL, label=u'87%',
name=u'q1a_lbl', parent=self.alg_panel, pos=wx.Point(115, 48),
size=wx.Size(23, 17), style=0)
self.q1a_lbl.SetToolTipString(u'Date \n\nNotes')
self.q1a_lbl.Bind(wx.EVT_LEFT_UP, self.OnQ1a_lblLeftUp)
Then I have the function:
def OnQ1a_lblLeftUp(self, event):
import quiz_notes
quiz_notes.create(self).Show(True)
Which works graphically, but I'm not really doing anything other than opening a window when the text is clicked on. Then I have another Frame with
import wx
def create(parent):
return quiz_notes(parent)
[wxID_QUIZ_NOTES, wxID_QUIZ_NOTESCANCEL_BTN, wxID_QUIZ_NOTESDATEPICKERCTRL1,
wxID_QUIZ_NOTESENTER_BTN, wxID_QUIZ_NOTESPANEL1, wxID_QUIZ_NOTESTEXTCTRL1,
] = [wx.NewId() for _init_ctrls in range(6)]
class quiz_notes(wx.Frame):
def _init_ctrls(self, prnt):
...and so on
I would like to pass at least a couple of variables. Eventually, when I start integrating the database into it, I would just pass a tuple. Or a reference to it. In C I'd just use a pointer. Anyway, make changes and then go back to my main window. In short, whats the best way to work with data between these two classes?
There are not pointers in Python, but there are mutable structures. You can share state between objects by handing the same state-object to multiple instances.
What that state-object should be depends entirely on what you're trying to do (I still have no idea). It could be anything mutable, a module, class, instance, dict or list.
In this example the shared state is a list in a global variable:
# a list is mutable
state = 'Hello World'.split()
class Class1:
def hi(self):
print ' '.join(state)
# do something to the shared state
state[0] = 'Bye'
class Class2:
def hi(self):
print ' '.join(state)
x = Class1()
y = Class2()
x.hi()
y.hi()
Check out Mike Driscoll's blog post on using PubSub in wxPython.
It's using the included PubSub in wxPython - just be aware that it is a stand-alone library, and the latest version's API is different to the one included in wx (a better API, if I may say so)
精彩评论