wxPython从框架到对话框来回传递一组参数的最佳方法

时间:2013-08-12 02:36:28

标签: python matplotlib wxpython

我是wxPython编程的新手,我理想的做法是在自定义对话框(ParameterDialog)中设置打开的参数,文本框的默认值已经填入默认参数值中设置的IMAGEFRAME。然后通过按OK或关闭/退出对话框框架,传回ParameterDialog对话框中更改的值或所有值。最好的方法是什么?或者有一个比使用对话框弹出框架更好的解决方案。

此外,我已经阅读过使用Show()而不是ShowModal()打开的无模式窗口。每当我使用Show()时,没有任何事情发生,而不是ShowModal()。我已经删除了下面的大部分代码,但是应该给出一个我想要的并且能够拼凑起来的最小例子。

import os
import pprint
import random
import wx
import numpy as np
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
    FigureCanvasWxAgg as FigCanvas, \
    NavigationToolbar2WxAgg as NavigationToolbar

class ImageFrame(wx.Frame):
    """ The main frame of the application
    """
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'title')

        self.param1 = 10
        self.param2 = 0.2

        self.panel = wx.Panel(self)        

        self.button_set_parameters = wx.Button(self.panel, -1, "Set Parameters")
        self.Bind(wx.EVT_BUTTON, self.on_set_parameters, self.button_set_parameters)

        #
        # Layout with box sizers
        #
        self.vbox = wx.BoxSizer(wx.VERTICAL)
        self.vbox.Add(self.button_set_parameters, 0, border=3, flag=flags)
        self.panel.SetSizer(self.vbox)
        self.vbox.Fit(self)

    def on_set_parameters(self, event):
        dia = ParameterDialog()
        res = dia.ShowModal() # Use Show() here to allow edit window to remain open while using rest of application

        # Ideally the code below would run on ok or on closing the dialog?? Is this possible or better way to do this? Or is Checking everytime a change is made to a textbox possible and a better way?
        if res == wx.ID_CLOSE or res == wx.ID_EXIT or res == wx.ID_OK:
            self.param1 = dia.param1.GetValue()
            self.param2 = dia.param2.GetValue()
        dia.Destroy()
        return True

    def on_exit(self, event):
        self.Destroy()


class ParameterDialog(wx.Dialog):
    """
    Used to set the parameters for running.
    """
    def __init__(self):
        wx.Dialog.__init__(self, None, title="Parameters")

        self.static_text_param1 = wx.StaticText(self, label="Param1:")
        # Defualt value of 10 displayed in the textbox here as param1 but would need passed in from the ImageFrame class.
        self.param1 = wx.TextCtrl(self, size=(100, -1))        

        self.static_param2 = wx.StaticText(self, label="Param2:")
        self.param2 = wx.TextCtrl(self, size=(100, -1))        

        # Setup up Sizer
        flags = wx.ALIGN_LEFT | wx.ALL | wx.ALIGN_CENTER_VERTICAL
        sizer_vert =  wx.BoxSizer(wx.VERTICAL)

        sizer_horz = wx.BoxSizer(wx.HORIZONTAL)
        sizer_horz.Add(self.static_text_param1, 0, border=3, flag=flags)
        sizer_horz.Add(self.param1, 0, border=3, flag=flags)
        sizer_vert.Add(sizer_horz, 0, flag = wx.ALIGN_LEFT | wx.TOP)

        sizer_horz = wx.BoxSizer(wx.HORIZONTAL)
        sizer_horz.Add(self.static_param2, 0, border=3, flag=flags)
        sizer_horz.Add(self.param2, 0, border=3, flag=flags)
        sizer_vert.Add(sizer_horz, 0, flag = wx.ALIGN_LEFT | wx.BOTTOM)

        self.SetSizer(sizer_vert)
        sizer_vert.Fit(self)


if __name__ == '__main__':
    app = wx.PySimpleApp()
    app.frame = ImageFrame()
    app.frame.Show()
    app.MainLoop()

1 个答案:

答案 0 :(得分:0)

您应该能够使用已有的值获取值:

self.param1 = dia.param1.GetValue()

但是,这仅在您以模态方式显示对话框时才有效。当您以模态方式显示对话框时,它会阻止应用程序的主循环并为对话框创建一个新的主循环。然后,当对话框退出时,您可以在销毁对话框之前使用上面的代码获取值。

如果您不想使用模态对话框或只是想尝试稍微不同的方式,我建议尝试使用pubsub。它使用发布/订阅模型,您可以在其中设置一个或多个侦听器,然后将消息发布到所述侦听器。这是一个简单教程的链接:http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/