Frage

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.

War es hilfreich?

Lösung

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()

Andere Tipps

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:

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top