此代码来自网站弹出窗口问题的答案之一。我想在单击textctrl时打开子帧,同时关闭主机,点击“保存并关闭”按钮后数据被传回主机,代码现在可以打开子窗口并关闭然而,每次我点击“保存并关闭”按钮时,主窗口会在一秒钟内出现,并再次显示子窗口。我假设焦点是自动设置到文本框,所以你可以帮助setfocus到主面板或大型机来解决问题。
import wx
from wx.lib.pubsub import Publisher
########################################################################
class OtherFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, wx.ID_ANY, "Secondary Frame")
panel = wx.Panel(self)
msg = "Enter a Message to send to the main frame"
instructions = wx.StaticText(panel, label=msg)
self.msgTxt = wx.TextCtrl(panel, value="")
closeBtn = wx.Button(panel, label="Send and Close")
closeBtn.Bind(wx.EVT_BUTTON, self.onSendAndClose)
sizer = wx.BoxSizer(wx.VERTICAL)
flags = wx.ALL|wx.CENTER
sizer.Add(instructions, 0, flags, 5)
sizer.Add(self.msgTxt, 0, flags, 5)
sizer.Add(closeBtn, 0, flags, 5)
panel.SetSizer(sizer)
#----------------------------------------------------------------------
def onSendAndClose(self, event):
"""
Send a message and close frame
"""
msg = self.msgTxt.GetValue()
Publisher().sendMessage(("show.mainframe"), msg)
self.Close()
########################################################################
class MainPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
self.frame = parent
Publisher().subscribe(self.showFrame, ("show.mainframe"))
self.pubsubText = wx.TextCtrl(self, value="")
self.pubsubText.Bind(wx.EVT_SET_FOCUS,self.hideFrame)
Btn = wx.Button(self, label="Change")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.pubsubText, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(Btn, 0, wx.ALL|wx.CENTER, 5)
self.SetSizer(sizer)
#----------------------------------------------------------------------
def hideFrame(self, event):
""""""
self.frame.Hide()
new_frame = OtherFrame()
new_frame.Show()
#----------------------------------------------------------------------
def showFrame(self, msg):
"""
Shows the frame and shows the message sent in the
text control
"""
self.pubsubText.SetValue(msg.data)
frame = self.GetParent()
frame.Show()
########################################################################
class MainFrame(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Pubsub Tutorial")
panel = MainPanel(self)
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()
答案 0 :(得分:0)
发生这种情况的原因是您绑定了错误的事件。只要文本控件处于焦点,EVT_SET_FOCUS
就会触发。单击它时它会聚焦。当你关闭第二帧并将第一帧重新带回来时,它也会重新聚焦,这就是你再次看到第二帧的原因。
相反,您应该绑定到wx.EVT_LEFT_DOWN
,这是一个鼠标事件。这是更新后的代码:
import wx
from wx.lib.pubsub import Publisher
########################################################################
class OtherFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, wx.ID_ANY, "Secondary Frame")
panel = wx.Panel(self)
msg = "Enter a Message to send to the main frame"
instructions = wx.StaticText(panel, label=msg)
self.msgTxt = wx.TextCtrl(panel, value="")
closeBtn = wx.Button(panel, label="Send and Close")
closeBtn.Bind(wx.EVT_BUTTON, self.onSendAndClose)
sizer = wx.BoxSizer(wx.VERTICAL)
flags = wx.ALL|wx.CENTER
sizer.Add(instructions, 0, flags, 5)
sizer.Add(self.msgTxt, 0, flags, 5)
sizer.Add(closeBtn, 0, flags, 5)
panel.SetSizer(sizer)
#----------------------------------------------------------------------
def onSendAndClose(self, event):
"""
Send a message and close frame
"""
msg = self.msgTxt.GetValue()
Publisher().sendMessage(("show.mainframe"), msg)
self.Close()
########################################################################
class MainPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
self.frame = parent
Publisher().subscribe(self.showFrame, ("show.mainframe"))
self.pubsubText = wx.TextCtrl(self, value="")
#### NOTE CHANGE HERE ####
self.pubsubText.Bind(wx.EVT_LEFT_DOWN,self.hideFrame)
Btn = wx.Button(self, label="Change")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.pubsubText, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(Btn, 0, wx.ALL|wx.CENTER, 5)
self.SetSizer(sizer)
#----------------------------------------------------------------------
def hideFrame(self, event):
""""""
self.frame.Hide()
new_frame = OtherFrame()
new_frame.Show()
#----------------------------------------------------------------------
def showFrame(self, msg):
"""
Shows the frame and shows the message sent in the
text control
"""
self.pubsubText.SetValue(msg.data)
frame = self.GetParent()
frame.Show()
########################################################################
class MainFrame(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Pubsub Tutorial")
panel = MainPanel(self)
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()