wxPython从帧到对话框来回传递一组参数的最佳方法
我是wxPython编程新手,理想情况下,我可以在自定义对话框(ParameterDialog)中打开参数,其中的文本框中已经填入了ImageFrame中设置的默认参数值的默认值。然后通过按OK或关闭/退出对话框框,传回ParameterDialog对话框框中更改的值或所有值。最好的办法是什么?或者有没有比使用对话框弹出框更好的解决方案 我还了解到,非模态窗口使用Show()而不是showmodel()打开。无论何时使用Show()而不是showmodel(),都不会发生任何事情。我已经删去了下面的大部分代码,但应该给出一个我想要的和能够拼凑在一起的最简单的例子wxPython从帧到对话框来回传递一组参数的最佳方法,python,matplotlib,wxpython,Python,Matplotlib,Wxpython,我是wxPython编程新手,理想情况下,我可以在自定义对话框(ParameterDialog)中打开参数,其中的文本框中已经填入了ImageFrame中设置的默认参数值的默认值。然后通过按OK或关闭/退出对话框框,传回ParameterDialog对话框框中更改的值或所有值。最好的办法是什么?或者有没有比使用对话框弹出框更好的解决方案 我还了解到,非模态窗口使用Show()而不是showmodel()打开。无论何时使用Show()而不是showmodel(),都不会发生任何事情。我已经删去了下
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()
您应该能够使用您已有的内容获取值:
self.param1 = dia.param1.GetValue()
但是,这仅在以模式显示对话框时有效。以模式显示对话框时,它会阻止应用程序的主循环,并为对话框创建新的主循环。然后,当对话框退出时,您可以在销毁对话框之前使用上面的代码获取值
如果您不想使用模态对话框,或者只是想尝试使用稍微不同的方式,我建议您尝试一下pubsub。它使用发布/订阅模型,在该模型中设置一个或多个侦听器,然后将消息发布到所述侦听器。这里有一个简单教程的链接:您可以用什么方法缩短示例?即使你说你已经把它编辑下来了,人们(包括我)可能不愿意读那么多代码。@tcaswell-我几乎删除了所有内容,除了对话框和框架之间传递的参数。我仍然在画这个数字,但这就是我剩下的全部。我希望这是最低限度的。如果您认为这样更好的话,我可以删除绘制图形,因此我只需要传递参数即可-许多代码也只是设置大小。感兴趣的核心代码可能是10-15行。然后只显示10-15行有趣的代码,周围的基础设施最少。大小调整器对参数传递有影响吗?绘图是否影响有关参数传递的任何内容?请看@tcaswell-我已经更新了它,但我想我已经把它整理好了。我刚创建了一个参数类。如果用户在对话框中保存或点击ok,则传入参数类并修改值。然后在点击save后销毁对话框。不确定这是最好的方式还是最具蟒蛇风格的方式。