wxpython 多个类之间的事件处理
我在 wxpython 中有两个类,一个 wx.Frame 类和一个 wx.Dialog (日历对话框)类
我的日历对话框如下所示:
class Calendar(wx.Dialog):
def __init__(self, parent, id, title):
wx.Dialog.__init__(self, parent, id, title)
vbox = wx.BoxSizer(wx.VERTICAL)
self.calend = cal.CalendarCtrl(self, -1, wx.DateTime_Now(),
style = cal.CAL_SHOW_HOLIDAYS|cal.CAL_SEQUENTIAL_MONTH_SELECTION)
vbox.Add(self.calend, 0, wx.EXPAND | wx.ALL, 20)
vbox.Add((-1, 20))
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
btn = wx.Button(self, -1, 'Ok')
cancelBtn = wx.Button(self, -1, 'Cancel')
hbox2.Add(btn, 1)
hbox2.Add(cancelBtn, 1)
vbox.Add(hbox2, 0, wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, 20)
btn.Bind(wx.EVT_BUTTON, self.okClicked)
cancelBtn.Bind(wx.EVT_BUTTON, self.OnQuit)
self.SetSizerAndFit(vbox)
self.Show(True)
self.Centre()
def okClicked(self, event):
date = self.calend.GetDate()
print date
return date
我从框架类中调用日历对话框,如下所示
def calClick1(self, event):
calObj = Calendar(None, -1, 'test cal')
calObj.ShowModal()
#here i want to set the returned date to a wx.TextCtrl
如何设置 TextCtrl 的值我的 wx.Frame 类中的框到日历对话框类中返回的日期?
I have two classes in wxpython, a wx.Frame class and a wx.Dialog (calendar dialog) class
My calendar dialog looks like this:
class Calendar(wx.Dialog):
def __init__(self, parent, id, title):
wx.Dialog.__init__(self, parent, id, title)
vbox = wx.BoxSizer(wx.VERTICAL)
self.calend = cal.CalendarCtrl(self, -1, wx.DateTime_Now(),
style = cal.CAL_SHOW_HOLIDAYS|cal.CAL_SEQUENTIAL_MONTH_SELECTION)
vbox.Add(self.calend, 0, wx.EXPAND | wx.ALL, 20)
vbox.Add((-1, 20))
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
btn = wx.Button(self, -1, 'Ok')
cancelBtn = wx.Button(self, -1, 'Cancel')
hbox2.Add(btn, 1)
hbox2.Add(cancelBtn, 1)
vbox.Add(hbox2, 0, wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, 20)
btn.Bind(wx.EVT_BUTTON, self.okClicked)
cancelBtn.Bind(wx.EVT_BUTTON, self.OnQuit)
self.SetSizerAndFit(vbox)
self.Show(True)
self.Centre()
def okClicked(self, event):
date = self.calend.GetDate()
print date
return date
And I call the calendar dialog from my frame class like so
def calClick1(self, event):
calObj = Calendar(None, -1, 'test cal')
calObj.ShowModal()
#here i want to set the returned date to a wx.TextCtrl
How do I set the value of a TextCtrl box in my wx.Frame class to the returned date in the calendar dialog class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 calClick1 中创建以 self 作为父级的 Calendar 实例:
在 Calendar 类中添加如下内容:
wx.Dialog.__init__(...)
之后的self.parent =parent
。现在您可以访问日历中框架的属性。示例:
如果 Frame 类中有类似以下内容:
您可以在 Calendar 类的 okClicked 方法中更改它:
In calClick1 make instance of Calendar with self as parent:
In Calendar class add something like:
self.parent = parent
afterwx.Dialog.__init__(...)
.Now you can access attributes of frame in calendar. Example:
If you have in Frame class something like:
You can change it in method okClicked of class Calendar:
正如 wxPython 演示中所演示的,当您调用 ShowModal() 时,应该在 if 语句中使用它:
现在,如果您要更改:
To:
那么您的日期变量将是该类的全局变量,您可以通过 calObj 从内部访问它框架,例如:calObj.date,
而不是您可以执行类似的操作,这将允许您在对话框关闭后对框架的 TextCtrl 进行更改,而不是回调到父级:< em>(当然,这并不是说有什么问题!;)
未经测试,但下面的内容应该可以帮助您:
As demonstrated in the wxPython demo, when you call ShowModal() it should be used within an if statement:
Now if you were to change:
To:
Than your date variable will be global to the class and you can access it through calObj from within the frame, like: calObj.date
than you could do something like this which will allow you to make the changes to the frame's TextCtrl after the Dialog has been closed, instead of calling back to the parent: (not that there is anything wrong with that of course! ;)
Untested, but the below should do the trick for you: