我面临一些问题需要从wxpython main fram获得返回值。
我的剧本:
import wx
import wx.xrc
class Email_template( wx.Frame ):
def __init__( self, parent, to, cc, subject, message_boby ):
app = wx.App(False)
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 650,500 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
self.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_INFOBK ) )
main_sizer = wx.FlexGridSizer( 0, 1, 0, 0 )
main_sizer.SetFlexibleDirection( wx.BOTH )
main_sizer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
header_sizer = wx.FlexGridSizer( 0, 2, 0, 0 )
header_sizer.SetFlexibleDirection( wx.BOTH )
header_sizer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
header_sizer_right = wx.FlexGridSizer( 0, 1, 0, 0 )
header_sizer_right.SetFlexibleDirection( wx.BOTH )
header_sizer_right.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
self.mail_send_button = wx.Button( self, wx.ID_ANY, u"Send", wx.DefaultPosition, wx.Size( -1,50 ), 0 )
header_sizer_right.Add( self.mail_send_button, 0, wx.ALL, 5 )
self.mail_edit_button = wx.Button( self, wx.ID_ANY, u"Edit", wx.DefaultPosition, wx.Size( -1,50 ), 0 )
header_sizer_right.Add( self.mail_edit_button, 0, wx.ALL, 5 )
header_sizer.Add( header_sizer_right, 1, wx.EXPAND, 5 )
header_sizer_left = wx.FlexGridSizer( 0, 2, 0, 0 )
header_sizer_left.SetFlexibleDirection( wx.BOTH )
header_sizer_left.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
self.from_staticText = wx.StaticText( self, wx.ID_ANY, u"From:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.from_staticText.Wrap( -1 )
header_sizer_left.Add( self.from_staticText, 0, wx.ALL, 5 )
self.from_textCtrl = wx.TextCtrl( self, wx.ID_ANY, "from_mail@mymail.com", wx.DefaultPosition, wx.Size( 400,-1 ), 0 )
header_sizer_left.Add( self.from_textCtrl, 0, wx.ALL, 5 )
self.to_staticText = wx.StaticText( self, wx.ID_ANY, u"To:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.to_staticText.Wrap( -1 )
header_sizer_left.Add( self.to_staticText, 0, wx.ALL, 5 )
self.to_textCtrl = wx.TextCtrl( self, wx.ID_ANY, to, wx.DefaultPosition, wx.Size( 400,-1 ), 0 )
header_sizer_left.Add( self.to_textCtrl, 0, wx.ALL, 5 )
self.cc_staticText = wx.StaticText( self, wx.ID_ANY, u"CC:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.cc_staticText.Wrap( -1 )
header_sizer_left.Add( self.cc_staticText, 0, wx.ALL, 5 )
self.cc_textCtrl = wx.TextCtrl( self, wx.ID_ANY, cc, wx.DefaultPosition, wx.Size( 400,-1 ), 0 )
header_sizer_left.Add( self.cc_textCtrl, 0, wx.ALL, 5 )
self.subject_staticText = wx.StaticText( self, wx.ID_ANY, u"Subject:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.subject_staticText.Wrap( -1 )
header_sizer_left.Add( self.subject_staticText, 0, wx.ALL, 5 )
self.subject_textCtrl = wx.TextCtrl( self, wx.ID_ANY, subject, wx.DefaultPosition, wx.Size( 400,-1 ), 0 )
header_sizer_left.Add( self.subject_textCtrl, 0, wx.ALL, 5 )
header_sizer.Add( header_sizer_left, 1, wx.EXPAND, 5 )
main_sizer.Add( header_sizer, 1, wx.EXPAND, 5 )
mail_sizer = wx.FlexGridSizer( 0, 1, 0, 0 )
mail_sizer.SetFlexibleDirection( wx.BOTH )
mail_sizer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
self.mail_textCtrl = wx.TextCtrl( self, wx.ID_ANY, message_boby, style=wx.TE_MULTILINE, size=( 600,330 ))
mail_sizer.Add( self.mail_textCtrl, 0, wx.ALL, 5 )
main_sizer.Add( mail_sizer, 1, wx.EXPAND, 5 )
self.to_textCtrl.Enable(False)
self.cc_textCtrl.Enable(False)
self.subject_textCtrl.Enable(False)
self.mail_textCtrl.Enable(False)
self.message_boby = message_boby
self.SetSizer( main_sizer )
self.Layout()
wx.EVT_BUTTON(self, self.mail_send_button.GetId(), self.OnClick_send)
wx.EVT_BUTTON(self, self.mail_edit_button.GetId(), self.OnClick_edit)
self.Centre( wx.BOTH )
self.Show(True)
app.MainLoop()
def OnClick_send(self, event):
self.to = self.to_textCtrl.GetValue()
self.cc = self.cc_textCtrl.GetValue()
self.subject = self.subject_textCtrl.GetValue()
self.message_boby = self.mail_textCtrl.GetValue()
r_value = self.message_boby
self.Destroy()
return r_value
def OnClick_edit(self, event):
self.to_textCtrl.Enable(True)
self.cc_textCtrl.Enable(True)
self.subject_textCtrl.Enable(True)
self.mail_textCtrl.Enable(True)
return_values = Email_template(None, "tomail@mymail.com", "cc_copy@mymail.com", "message subject", "mail")
print return_values
显示以下错误消息:
wxPython wrapper for DELETED Email_template object! (The C++ object no longer exists.)
但我需要打印来自gui的message_body更新内容。
感谢。
答案 0 :(得分:1)
首先,我更改了你的sizer。我用一个非常强大的GridBagSizer
替换了它们。我还删除了大多数小部件大小参数,因为sizer现在将根据窗口大小处理小部件大小。我还从你的wx调用中删除了很多参数,因为它们是默认的。通过删除默认的可选参数,代码更具可读性。您wx.ID_ANY
中的wx.DefaultSize
大部分都不需要像wx.DefaultPosition
中那样wx.EmptyString
,wxPython
,wxWidgets
或import wx
class EmailTemplate(wx.Frame):
def __init__(self, to, cc, subject, message_body):
wx.Frame.__init__(self, None, size=(650, 500))
self.panel = wx.Panel(self)
self.panel.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INFOBK))
self.sizer = wx.GridBagSizer(5, 5)
self.sizer.AddGrowableCol(3)
self.sizer.AddGrowableRow(5)
# Add spacers. Not the best, but avoids another sizer for border
self.sizer.Add((0, 0), pos=(0, 4))
self.sizer.Add((0, 0), pos=(6, 0))
self.mail_send_button = wx.Button(self.panel, label="Send")
self.mail_send_button.Bind(wx.EVT_BUTTON, self.OnClick_send)
self.sizer.Add(self.mail_send_button, pos=(1, 1), span=(2, 1), flag=wx.EXPAND)
self.mail_edit_button = wx.Button(self.panel, label="Edit")
self.mail_edit_button.Bind(wx.EVT_BUTTON, self.OnClick_edit)
self.sizer.Add(self.mail_edit_button, pos=(3, 1), span=(2, 1), flag=wx.EXPAND)
self.from_staticText = wx.StaticText(self.panel, label="From:")
self.sizer.Add(self.from_staticText, pos=(1, 2), flag=wx.ALIGN_CENTER_VERTICAL)
self.from_textCtrl = wx.TextCtrl(self.panel, value="from_mail@mymail.com")
self.sizer.Add(self.from_textCtrl, pos=(1, 3), flag=wx.EXPAND)
self.to_staticText = wx.StaticText(self.panel, label="To:")
self.sizer.Add(self.to_staticText, pos=(2, 2), flag=wx.ALIGN_CENTER_VERTICAL)
self.to_textCtrl = wx.TextCtrl(self.panel, value=to)
self.sizer.Add(self.to_textCtrl, pos=(2, 3), flag=wx.EXPAND)
self.cc_staticText = wx.StaticText(self.panel, label="CC:")
self.sizer.Add(self.cc_staticText, pos=(3, 2), flag=wx.ALIGN_CENTER_VERTICAL)
self.cc_textCtrl = wx.TextCtrl(self.panel, value=cc)
self.sizer.Add(self.cc_textCtrl, pos=(3, 3), flag=wx.EXPAND)
self.subject_staticText = wx.StaticText(self.panel, label="Subject:")
self.sizer.Add(self.subject_staticText, pos=(4, 2), flag=wx.ALIGN_CENTER_VERTICAL)
self.subject_textCtrl = wx.TextCtrl(self.panel, value=subject)
self.sizer.Add(self.subject_textCtrl, pos=(4, 3), flag=wx.EXPAND)
self.mail_textCtrl = wx.TextCtrl(self.panel, value=message_body, style=wx.TE_MULTILINE)
self.sizer.Add(self.mail_textCtrl, pos=(5, 1), span=(1, 3), flag=wx.EXPAND)
self.panel.SetSizerAndFit(self.sizer)
self.to_textCtrl.Disable()
self.cc_textCtrl.Disable()
self.subject_textCtrl.Disable()
self.mail_textCtrl.Disable()
self.Show()
def OnClick_send(self, event):
self.DoSomethingWithEmail(self.to_textCtrl.GetValue(),
self.cc_textCtrl.GetValue(),
self.subject_textCtrl.GetValue(),
self.mail_textCtrl.GetValue())
self.Close()
def OnClick_edit(self, event):
self.to_textCtrl.Enable(True)
self.cc_textCtrl.Enable(True)
self.subject_textCtrl.Enable(True)
self.mail_textCtrl.Enable(True)
def DoSomethingWithEmail(self, to, cc, subject, text):
# Maybe send here?
print to, cc, subject, text
app = wx.App(False)
window = EmailTemplate("tomail@mymail.com", "cc_copy@mymail.com", "message subject", "mail")
app.MainLoop()
等。
其次,您编写应用程序的方式很奇怪。我想你想要两个中的任何一个:
1)创建一个GUI应用程序,它有一个窗口,允许您编辑,在按发送时执行某些操作。当你在案件中按发送时,它也会自动关闭,所以也许这不是你所追求的......
import wx
class EmailTemplate(wx.Dialog):
def __init__(self, to, cc, subject, message_body):
wx.Dialog.__init__(self, None, size=(650, 500))
self.panel = wx.Panel(self)
self.panel.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INFOBK))
self.main_sizer = wx.BoxSizer()
self.sizer = wx.GridBagSizer(5, 5)
self.sizer.AddGrowableCol(3)
self.sizer.AddGrowableRow(5)
# Add spacers. Not the best, but avoids another sizer for border
self.sizer.Add((0, 0), pos=(0, 4))
self.sizer.Add((0, 0), pos=(6, 0))
self.mail_send_button = wx.Button(self.panel, label="Send")
self.mail_send_button.Bind(wx.EVT_BUTTON, self.OnClick_send)
self.sizer.Add(self.mail_send_button, pos=(1, 1), span=(2, 1), flag=wx.EXPAND)
self.mail_edit_button = wx.Button(self.panel, label="Edit")
self.mail_edit_button.Bind(wx.EVT_BUTTON, self.OnClick_edit)
self.sizer.Add(self.mail_edit_button, pos=(3, 1), span=(2, 1), flag=wx.EXPAND)
self.from_staticText = wx.StaticText(self.panel, label="From:")
self.sizer.Add(self.from_staticText, pos=(1, 2), flag=wx.ALIGN_CENTER_VERTICAL)
self.from_textCtrl = wx.TextCtrl(self.panel, value="from_mail@mymail.com")
self.sizer.Add(self.from_textCtrl, pos=(1, 3), flag=wx.EXPAND)
self.to_staticText = wx.StaticText(self.panel, label="To:")
self.sizer.Add(self.to_staticText, pos=(2, 2), flag=wx.ALIGN_CENTER_VERTICAL)
self.to_textCtrl = wx.TextCtrl(self.panel, value=to)
self.sizer.Add(self.to_textCtrl, pos=(2, 3), flag=wx.EXPAND)
self.cc_staticText = wx.StaticText(self.panel, label="CC:")
self.sizer.Add(self.cc_staticText, pos=(3, 2), flag=wx.ALIGN_CENTER_VERTICAL)
self.cc_textCtrl = wx.TextCtrl(self.panel, value=cc)
self.sizer.Add(self.cc_textCtrl, pos=(3, 3), flag=wx.EXPAND)
self.subject_staticText = wx.StaticText(self.panel, label="Subject:")
self.sizer.Add(self.subject_staticText, pos=(4, 2), flag=wx.ALIGN_CENTER_VERTICAL)
self.subject_textCtrl = wx.TextCtrl(self.panel, value=subject)
self.sizer.Add(self.subject_textCtrl, pos=(4, 3), flag=wx.EXPAND)
self.mail_textCtrl = wx.TextCtrl(self.panel, value=message_body, style=wx.TE_MULTILINE)
self.sizer.Add(self.mail_textCtrl, pos=(5, 1), span=(1, 3), flag=wx.EXPAND)
self.panel.SetSizer(self.sizer)
self.main_sizer.Add(self.panel, 1, flag=wx.EXPAND)
self.SetSizer(self.main_sizer)
self.to_textCtrl.Disable()
self.cc_textCtrl.Disable()
self.subject_textCtrl.Disable()
self.mail_textCtrl.Disable()
def OnClick_send(self, event):
self.to = self.to_textCtrl.GetValue()
self.cc = self.cc_textCtrl.GetValue()
self.subject = self.subject_textCtrl.GetValue()
self.body = self.mail_textCtrl.GetValue()
self.EndModal(wx.ID_OK)
def OnClick_edit(self, event):
self.to_textCtrl.Enable(True)
self.cc_textCtrl.Enable(True)
self.subject_textCtrl.Enable(True)
self.mail_textCtrl.Enable(True)
def GetValuesAsDict(self):
d = {"to": self.to,
"cc": self.cc,
"subject": self.subject,
"body": self.body}
return d
app = wx.App(False)
dialog = EmailTemplate("tomail@mymail.com", "cc_copy@mymail.com", "message subject", "mail")
result = dialog.ShowModal()
if result == wx.ID_OK:
# Maybe send here instead of print?
print dialog.GetValuesAsDict()
2)只创建一个对话框,显示对话框,让用户关闭它(使用发送)并使用Python脚本而不需要真正的GUI应用程序
{{1}}
请注意代码组成方式的不同之处。在您的情况下,差异可能很微妙,但如果您决定构建更大的东西,则会更加重要。这也是使用wxPython的惯用方法,因此其他程序员会发现更容易理解你的代码。