是否有可能使wxFrame对象的行为类似于模式对话框,因为创建wxFrame对象的窗口将停止执行直到wxFrame对象退出?
我正在处理small game,并遇到以下问题。我有一个承载主应用程序(策略部分)的主程序窗口。有时,我需要将控制权转移到第二个窗口,以解决游戏部分(战术部分)的问题。在第二个窗口中时,我希望第一个窗口中的处理停止并等待第二个窗口中完成的工作。
通常,模态对话框可以解决问题,但是我希望新窗口具有一些wxDialog似乎无法提供的功能,即底部的状态栏以及能够调整/最大化/最小化窗口的功能(这应该是可能的,但是不起作用,请参阅此问题How to get the minimize and maximize buttons to appear on a wxDialog object)。
作为补充,我希望第二个窗口的功能需要与主窗口保持完全脱钩,因为它将最终分解为一个单独的程序。
有人这样做或有任何建议吗?
我也在寻找类似的解决方案,并提出了这个解决方案,创建框架,通过执行frame.MakeModal()禁用其他窗口,并在显示框架后停止执行开始和事件循环,并在框架关闭时退出事件循环例如我在这里是使用wxpython的示例,但在wxwidgets中应该类似。
import wx
class ModalFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, style=wx.DEFAULT_FRAME_STYLE|wx.STAY_ON_TOP)
btn = wx.Button(self, label="Close me")
btn.Bind(wx.EVT_BUTTON, self.onClose)
self.Bind(wx.EVT_CLOSE, self.onClose) # (Allows main window close to work)
def onClose(self, event):
self.MakeModal(False) # (Re-enables parent window)
self.eventLoop.Exit()
self.Destroy() # (Closes window without recursion errors)
def ShowModal(self):
self.MakeModal(True) # (Explicit call to MakeModal)
self.Show()
# now to stop execution start a event loop
self.eventLoop = wx.EventLoop()
self.eventLoop.Run()
app = wx.PySimpleApp()
frame = wx.Frame(None, title="Test Modal Frame")
btn = wx.Button(frame, label="Open modal frame")
def onclick(event):
modalFrame = ModalFrame(frame, "Modal Frame")
modalFrame.ShowModal()
print "i will get printed after modal close"
btn.Bind(wx.EVT_BUTTON, onclick)
frame.Show()
app.SetTopWindow(frame)
app.MainLoop()
“停止执行”窗口实际上没有意义,因为该窗口仅处理发送给它的事件,例如鼠标,键盘或绘画事件,而忽略它们会使程序看起来好像挂起了。您应该做的是禁用框架中的所有控件,这将使它们变灰,并使用户意识到目前无法与它们进行交互。
您还可以完全禁用父框架,而不是禁用其上的所有控件。查看wxWindowDisabler类,构造函数具有一个参数,指示可以与之交互的窗口,并且该应用程序的所有其他窗口将被禁用。
如果以后要执行辅助程序,则可以使用wxExecute()函数来执行它。
[这花了我大量的时间,但这是从Anurag的例子中衍生出来的一个有效的例子:
import wx
class ChildFrame(wx.Frame):
''' ChildFrame launched from MainFrame '''
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, -1,
title=self.__class__.__name__,
size=(300,150))
panel = wx.Panel(self, -1)
closeButton = wx.Button(panel, label="Close Me")
self.Bind(wx.EVT_BUTTON, self.__onClose, id=closeButton.GetId())
self.Bind(wx.EVT_CLOSE, self.__onClose) # (Allows frame's title-bar close to work)
self.CenterOnParent()
self.GetParent().Enable(False)
self.Show(True)
self.__eventLoop = wx.EventLoop()
self.__eventLoop.Run()
def __onClose(self, event):
self.GetParent().Enable(True)
self.__eventLoop.Exit()
self.Destroy()
class MainFrame(wx.Frame):
''' Launches ChildFrame when button is clicked. '''
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id,
title=self.__class__.__name__,
size=(400, 300))
panel = wx.Panel(self, -1)
launchButton = wx.Button(panel, label="launch modal window")
self.Bind(wx.EVT_BUTTON, self.__onClick, id=launchButton.GetId())
self.Centre()
self.Show(True)
def __onClick(self, event):
dialog = ChildFrame(self, -1)
print "I am printed by MainFrame and get printed after ChildFrame is closed"
if __name__ == '__main__':
app = wx.App()
frame = MainFrame(None, -1)
frame.Show()
app.MainLoop()
不确定这是否是一个很好的答案,但它确实有效。
bool WinApp1::OnInit()
{
if (!wxApp::OnInit())
return false;
SettingsDialog dialog(m_settingsData);
dialog.ShowModal();
return false;
}
SettingsDialog::SettingsDialog(SettingsData& settingsData)
: m_settingsData(settingsData)
{
SetExtraStyle(wxDIALOG_EX_CONTEXTHELP);
wxWindow* parent = nullptr;
Create(parent, wxID_ANY, "Preferences", wxDefaultPosition, wxDefaultSize,
wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
WinApp1窗口永远不会被赋予wxFrame,也不会绘制。