• wxPython Modal Dialog 模式对话框


    《wxPython in Action》chap 9 笔记

    1. Modal Dialog(模式对话框)

    A modal dialog blocks other widgets from receiving user events until it is closed;

    in other words, it places the user in dialog mode for the duration of its existence.

    模式对话框阻塞了别的窗口部件接收用户事件,直到该模式对话框被关闭。

    严格来说,dialog 与 frame 在外观上没有区别,只是处理事件(event)的方式不一样。

    通常,dialog 在外观上要稍微简单一点,所以很少使用 wx.Panel。

    wx.Dialog 具备 wx.Panel 的性质,widgets 一般直接添加到 wx.Dialog 中即可。

    2. 展示与关闭 Dialog

    Modal Dialog 与 普通的 Frame 用法略有差别。

    最简单的示例代码如下:

    import wx
    
    class SubclassDialog(wx.Dialog):
        def __init__(self):
            wx.Dialog.__init__(self, None, -1, 'Dialog Subclass', size=(300, 100))
            okButton = wx.Button(self, wx.ID_OK, "OK", pos=(15, 15))
            okButton.SetDefault()
            cancelButton = wx.Button(self, wx.ID_CANCEL, "Cancel", pos=(115, 15))
    
    if __name__ == '__main__':
        app = wx.PySimpleApp()
        dialog = SubclassDialog()
        result = dialog.ShowModal()
        if result == wx.ID_OK:
            print "OK"
        else:
            print "Cancel"
        dialog.Destroy()

    2.1 展示 Dialog

    展示 Dialog 使用 ShowModal(),程序会等待 ShowModal() 执行结束并获取其返回值。

    此时,Dialog 是 wxPython 应用接受用户事件的唯一组件。其他应用不受影响。

    2.2 结束 Dialog Mode

    调用 EndModal(retCode) 方法可以关闭(close)Dialog Mode,retCode 是 int 值,被 ShowModal() 返回。

    此时,只是结束了 Dialog Mode,并未摧毁 Dialog。可以在其他地方继续展示该 Dialog。

    通常在 event 的 handler 中调用 EndModal()。

    2.3 摧毁 Dialog

    调用 Destroy() 方法

    2.4 典型用法

    dialog = SubclassDialog()
    result = dialog.ShowModal()  # 展示对话框,并等待结果
    if result == wx.ID_OK:  # 判断结果,并执行相应处理
        print "OK"
    else:
        print "Cancel"
    dialog.Destroy()  # 摧毁 Dialog

    2.5 自定义 event 的 handler,一般需要调用 EndModal()

    例如,用于连接数据库的 handler 实现如下:

    def OnConnect(self, event=None):
        host = self.input_host.GetValue()
        port = int(self.input_port.GetValue())
        if db.connect(host, port):  # connected
            self.EndModal(wx.ID_OK)
        else:
            msg_error = 'Error connecting to host(%s)' % host
            wx.MessageBox(msg_error, 'Error', wx.OK | wx.ICON_ERROR)
  • 相关阅读:
    hadoop 2.6.0 LightWeightGSet源码分析
    推断扫描后的内容是否是URL
    Merge Sorted Array
    带条件的分页【重点】
    第8章2节《MonkeyRunner源代码剖析》MonkeyRunner启动执行过程-解析处理命令行參数
    php持续集成环境笔记
    hdu5137 How Many Maos Does the Guanxi Worth(单源最短路径)
    求最长公共子序列
    集团公司(嵌入ETL工具)財务报表系统解决方式
    ADT+NDK搭建jni编译环境
  • 原文地址:https://www.cnblogs.com/misspy/p/3648500.html
Copyright © 2020-2023  润新知