wxpython 多个类之间的事件处理

发布于 2024-10-24 05:09:42 字数 1317 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失而复得 2024-10-31 05:09:42

在 calClick1 中创建以 self 作为父级的 Calendar 实例:

calObj = Calendar(self, -1, 'test cal')

在 Calendar 类中添加如下内容:
wx.Dialog.__init__(...) 之后的 self.parent =parent
现在您可以访问日历中框架的属性。示例:

如果 Frame 类中有类似以下内容:

self.txt = wx.TextCtrl(self, -1, 'sample txt')

您可以在 Calendar 类的 okClicked 方法中更改它:

self.parent.txt.SetValue('some new string value')

In calClick1 make instance of Calendar with self as parent:

calObj = Calendar(self, -1, 'test cal')

In Calendar class add something like:
self.parent = parent after wx.Dialog.__init__(...).
Now you can access attributes of frame in calendar. Example:

If you have in Frame class something like:

self.txt = wx.TextCtrl(self, -1, 'sample txt')

You can change it in method okClicked of class Calendar:

self.parent.txt.SetValue('some new string value')
微凉徒眸意 2024-10-31 05:09:42

正如 wxPython 演示中所演示的,当您调用 ShowModal() 时,应该在 if 语句中使用它:

if calObj.ShowModal() == wx.ID_OK:

现在,如果您要更改:

def okClicked(self, event):
    date = self.calend.GetDate()
    print date
    return date

To:

def okClicked(self, event):
    self.date = self.calend.GetDate()
    print self.date
    return self.date

那么您的日期变量将是该类的全局变量,您可以通过 calObj 从内部访问它框架,例如:calObj.date

而不是您可以执行类似的操作,这将允许您在对话框关闭后对框架的 TextCtrl 进行更改,而不是回调到父级:< em>(当然,这并不是说有什么问题!;)

未经测试,但下面的内容应该可以帮助您:

# Dialog
class Calendar(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title)

        self.date = wx.DateTime_Now() #create global variable 'self.date'

        vbox = wx.BoxSizer(wx.VERTICAL)
        self.calend = cal.CalendarCtrl(self, -1, self.date, #from 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, wx.ID_OK, 'Ok') #changed id to wx.ID_OK
        cancelBtn = wx.Button(self, wx.ID_CANCEL, 'Cancel') #id to wx.ID_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):
        self.date = self.calend.GetDate() #set global value 'date' to selection
        print self.date
        return self.date


#Frame function
def calClick1(self, event):
    calObj = Calendar(None, -1, 'test cal')
    #if you clicked ok, continue...
    if calObj.ShowModal() == wx.ID_OK:
        # Set the TextCtrl by calling the global variable 'calObj.date'
        self.MyTextCtrl.SetValue(calObj.date)

As demonstrated in the wxPython demo, when you call ShowModal() it should be used within an if statement:

if calObj.ShowModal() == wx.ID_OK:

Now if you were to change:

def okClicked(self, event):
    date = self.calend.GetDate()
    print date
    return date

To:

def okClicked(self, event):
    self.date = self.calend.GetDate()
    print self.date
    return self.date

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:

# Dialog
class Calendar(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title)

        self.date = wx.DateTime_Now() #create global variable 'self.date'

        vbox = wx.BoxSizer(wx.VERTICAL)
        self.calend = cal.CalendarCtrl(self, -1, self.date, #from 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, wx.ID_OK, 'Ok') #changed id to wx.ID_OK
        cancelBtn = wx.Button(self, wx.ID_CANCEL, 'Cancel') #id to wx.ID_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):
        self.date = self.calend.GetDate() #set global value 'date' to selection
        print self.date
        return self.date


#Frame function
def calClick1(self, event):
    calObj = Calendar(None, -1, 'test cal')
    #if you clicked ok, continue...
    if calObj.ShowModal() == wx.ID_OK:
        # Set the TextCtrl by calling the global variable 'calObj.date'
        self.MyTextCtrl.SetValue(calObj.date)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文