What event should I bind to before enabling a button in wxPython?

妖精的绣舞 提交于 2019-12-13 01:29:55

问题


I have a simple app that requires 4 file inputs before proceeding to generate an output. Consider btnFile1 btnFile2 btnFile3 and btnFile4 and btnOutput.

btnOutput is disabled on init. btnFile1...4 all link to four similar, yet different, methods, call them OnBtn1..4 that assign a variable (call them file1..file4) to the file selected using the wx.FileDialog method.

I have a helper method, EnableButton() with a simple test:

if self.file1 and self.file2 and self.file3 and self.file4:
        self.btnGenerate.Enable()
    e.Skip()

What is the best way to call this method when all four variables are assigned? The four buttons can be clicked in any order, so we can't assign it only to the 4th button.

One obvious option is to add a call to EnableButton in every OnBtn method, but is this the ideal solution? What if there were 50 buttons?

Another option was to add a new event to each button like so:

for btn in (btn1, btn2, btn3, btn4):
    btn.Bind(wx.EVT_BUTTON, self.EnableButton)

However, this doesn't work with the method above because when the 4th button is clicked, the variable is not yet assigned. It is only assigned after the file is selected from wx.FileDialog.

I am sure there's a third, more ideal, solution that I am not seeing.


回答1:


The event EVT_UPDATE_UI is an event handler to look at the state of the application and change UI elements accordingly. You can use it to enable or disable your button depending on the state of the files, heres an example using check boxes to represent the file states, the button will only be enabled while all four check boxes are ticked.

import wx
from wx.lib import sized_controls


class TestFrame(sized_controls.SizedFrame):
    def __init__(self, *args, **kwargs):
        super(TestFrame, self).__init__(*args, **kwargs)
        contents_pane = self.GetContentsPane()
        self.files = {}
        for number in range(1, 5):
            label = 'file{}'.format(number)
            ctrl = wx.CheckBox(contents_pane, label=label)
            self.files[label] = ctrl

        btn_output = wx.Button(contents_pane, label='btn_output')
        btn_output.Bind(wx.EVT_UPDATE_UI, self.btn_update)

    def btn_update(self, event):
        enable = all(ctrl.IsChecked() for ctrl in self.files.values())
        event.Enable(enable)


app = wx.App(False)
test_frame = TestFrame(None)
test_frame.Show()
app.MainLoop()



回答2:


I would use a wx.Timer and set the timer to fire once a second. Every second it fires and calls your EnableButton event handler. The code would look something like this:

self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.EnableButton, self.timer)
self.timer.Start(1000)

In the event handler, I would add the following inside the if statement:

self.timer.Stop()

You may also want to add some logic to stop the timer when the user closes the application as timer objects can cause the application to not close correctly if they are still active. You can read more about timers here:

  • http://www.blog.pythonlibrary.org/2009/08/25/wxpython-using-wx-timers/


来源:https://stackoverflow.com/questions/23116617/what-event-should-i-bind-to-before-enabling-a-button-in-wxpython

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!