0

我是 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()
4

1 回答 1

0

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

self.param1 = dia.param1.GetValue()

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

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

于 2013-08-12T18:13:17.823 回答