如何使wxFrame行为像一个模态wxDialog对象 [英] How to make a wxFrame behave like a modal wxDialog object
问题描述
有可能使wxFrame对象的行为像一个模态对话框,因为创建wxFrame对象的窗口停止执行,直到wxFrame对象退出?
Is is possible to make a wxFrame object behave like a modal dialog box in that the window creating the wxFrame object stops execution until the wxFrame object exits?
在小型游戏上工作,遇到以下问题。我有一个主程序窗口主持主要应用程序(战略部分)。偶尔,我需要将控制转移到第二个窗口,以解决部分游戏(战术部分)。在第二个窗口中,我希望第一个窗口中的处理停止,等待完成第二个窗口中的工作。
I'm working on a small game and have run into the following problem. I have a main program window that hosts the main application (strategic portion). Occasionally, I need to transfer control to a second window for resolution of part of the game (tactical portion). While in the second window, I want the processing in the first window to stop and wait for completion of the work being done in the second window.
通常模态对话框做的技巧,但我想要的新窗口有一些功能,我似乎无法得到一个wxDialog,即在底部的状态栏和能够调整/最大化/最小化窗口(这应该是可能的,但不工作,请参阅此问题如何获取最小化和最大化按钮出现在wxDialog对象上)。
Normally a modal dialog would do the trick but I want the new window to have some functionality that I can't seem to get with a wxDialog, namely a status bar at the bottom and the ability to resize/maximize/minimize the window (this should be possible but doesn't work, see this question How to get the minimize and maximize buttons to appear on a wxDialog object).
作为补充说明,我想要第二个窗口的功能需要保持完全从主窗口解耦,因为它将被分离到一个单独的程序最终。
As an addition note, I want the second window's functionality needs to stay completely decoupled from the primary window as it will be spun off into a separate program eventually.
有人做过这个或有任何建议吗?
Has anyone done this or have any suggestions?
推荐答案
从类似的解决方案,并提出了这个解决方案,创建一个框架,通过执行frame.MakeModal()禁用其他窗口,并显示框架后,停止执行开始和事件循环,当框架关闭退出事件循环例如我这里是使用wxpython的示例,但它应该类似于wxwidgets。
I was also looking from similar solution and have comeup with this solution, create a frame, disable other windows by doing frame.MakeModal() and to stop execution start and event loop after showing frame, and when frame is closed exit the event loop e.g. I here is sample using wxpython but it should be similar in 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()
这篇关于如何使wxFrame行为像一个模态wxDialog对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!