在 wxPython 中启用按钮之前我应该​​绑定什么事件? [英] What event should I bind to before enabling a button in wxPython?

查看:25
本文介绍了在 wxPython 中启用按钮之前我应该​​绑定什么事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用程序,在继续生成输出之前需要 4 个文件输入.考虑 btnFile1 btnFile2 btnFile3btnFile4btnOutput.

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 在初始化时被禁用.btnFile1...4 都链接到四个相似但不同的方法,将它们称为 OnBtn1..4 分配变量(称为 file1..file4) 到使用 wx.FileDialog 方法选择的文件.

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.

我有一个辅助方法,EnableButton() 和一个简单的测试:

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

当所有四个变量都被赋值时,调用这个方法的最佳方式是什么?这四个按钮可以按任意顺序单击,因此我们不能仅将其分配给第 4 个按钮.

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.

一个明显的选择是在每个 OnBtn 方法中添加对 EnableButton 的调用,但这是理想的解决方案吗?如果有 50 个按钮会怎样?

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)

然而,这不适用于上面的方法,因为当点击第四个按钮时,变量尚未分配.仅在从 wx.FileDialog 中选择文件后才分配.

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.

推荐答案

事件 EVT_UPDATE_UI 是一个事件处理程序,用于查看应用程序的状态并相应地更改 UI 元素.您可以使用它来根据文件的状态启用或禁用您的按钮,这是一个使用复选框来表示文件状态的示例,只有在所有四个复选框都被勾选时才会启用该按钮.

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

这篇关于在 wxPython 中启用按钮之前我应该​​绑定什么事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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