从 wxPython 事件处理程序中调用函数 [英] Call functions from within a wxPython event handler

查看:28
本文介绍了从 wxPython 事件处理程序中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力寻找一种在 wxPython 事件处理函数中使用函数的方法.假设我有一个按钮,单击该按钮时,它会使用事件处理程序运行一个名为 OnRun 的函数.但是,用户忘记在 OnRun 按钮之前单击 RadionButton,我想弹出一个 MessageDialog 告诉他们他们忘记了一个步骤.我将多次重用此 MessageDialog,因此,与其复制/粘贴相同的代码,不如将这个 MessageDialog 放在一个函数中,并在用户忘记检查 RadioButton 时调用此 MessageDialog 函数.

I'm struggling to find a way to use function from within a wxPython event handler function. Say I have a button that when clicked it runs a function called OnRun using an event handler. However, the user forgot to click a RadionButton before the OnRun button and I want to pop-up a MessageDialog telling them they forgot a step. I'm going to reuse this MessageDialog several times, thus rather than doing a copy/paste of the same code I would like to just have this MessageDialog in a function and call this MessageDialog function if the user forgets to check a RadioButton.

如果这不是在事件处理程序中使用的函数,我知道我可以简单地将该函数作为参数,但我没有看到可以用这些来做到这一点的方法.任何帮助在这里将不胜感激.

If this wasn't a function used in an Event Handler I know I could simply put the function as an argument but I'm not seeing a way I can do this with these. Any help here would be appreciated.

推荐答案

以下代码展示了如何创建一个小方法,您可以重复使用该方法来显示自定义对话框并告诉用户他们需要接受协议.当然,您可以更改条件以执行任何您想要的操作.并且您可以更改showMsg"方法,以便只需稍作调整即可更改图标.

The following code shows how to create a little method that you can reuse to show custom dialogs and tells the user that they need to accept the agreement. You can change the conditionals to do whatever you want, of course. And you can change the "showMsg" method so that the icon changes too with just a little tweaking.

import wx

########################################################################
class TestFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Test")

        panel = wx.Panel(self)
        self.radios = wx.RadioBox(panel, label="Choices",
                                  choices = ["None", "Accept", "Reject"])

        button = wx.Button(panel, label="Run")
        button.Bind(wx.EVT_BUTTON, self.onBtn)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.radios, 0, wx.ALL, 5)
        sizer.Add(button, 0, wx.ALL, 5)
        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onBtn(self, event):
        """"""
        btn = event.GetEventObject()
        btn.SetLabel("Running")
        radio_value = self.radios.GetStringSelection()
        if radio_value == "None":
            self.showMsg("Error", "Please Choose 'Accept' or 'Reject'!")
        elif radio_value == "Accept":
            self.showMsg("Message", "Thank you for accepting!")
        else:
            self.showMsg("Message", "We're sorry, but you cannot continue the install")

    #----------------------------------------------------------------------
    def showMsg(self, title, msg):
        """"""
        dlg = wx.MessageDialog(None, msg, title, wx.OK | wx.ICON_QUESTION)
        dlg.ShowModal()
        dlg.Destroy()



if __name__ == "__main__":
    app = wx.App(False)
    frame = TestFrame()
    frame.Show()
    app.MainLoop()

这篇关于从 wxPython 事件处理程序中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆