Python 如何在带有子对话框的框架上应用MakeModel()方法
我在Python27上使用WxPythonClassic。我的文件结构如下(见下面的代码片段):- frame.py=此文件包含frame和dialog的两个类(分别为MyFrame1和MyDialog1) main.py=此文件导入上述类,并包含基于上述类的两个子类(分别为MyFrame2和MyDialog2) 现在保持文件结构如上所述,如何在对话框窗口打开时使框架窗口处于非活动状态 如何在上面的文件排列/结构上正确应用MakeModal()方法?到目前为止,我发现的所有示例都使用了两个框架,而不是一个框架和一个对话框 frame.pyPython 如何在带有子对话框的框架上应用MakeModel()方法,python,wxpython,wxwidgets,wxformbuilder,Python,Wxpython,Wxwidgets,Wxformbuilder,我在Python27上使用WxPythonClassic。我的文件结构如下(见下面的代码片段):- frame.py=此文件包含frame和dialog的两个类(分别为MyFrame1和MyDialog1) main.py=此文件导入上述类,并包含基于上述类的两个子类(分别为MyFrame2和MyDialog2) 现在保持文件结构如上所述,如何在对话框窗口打开时使框架窗口处于非活动状态 如何在上面的文件排列/结构上正确应用MakeModal()方法?到目前为止,我发现的所有示例都使用了两个框架,
import wx
# ************ FRAME 1 ************ #
# ************ FRAME 1 ************ #
class MyFrame1 ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
bSizer = wx.BoxSizer( wx.VERTICAL )
self.child_button = wx.Button( self, wx.ID_ANY, u"Child Frame", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer.Add( self.child_button, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )
self.SetSizer( bSizer )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.child_button.Bind( wx.EVT_BUTTON, self.On_child_button )
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def On_child_button( self, event ):
event.Skip()
# ************ DIALOG 1 ************ #
# ************ DIALOG 1 ************ #
class MyDialog1 ( wx.Dialog ):
def __init__( self, parent ):
wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 300,200 ), style = wx.DEFAULT_DIALOG_STYLE )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
self.Centre( wx.BOTH )
# Connect Events
self.Bind( wx.EVT_CLOSE, self.onClose )
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def onClose( self, event ):
event.Skip()
import wx
from frame import MyFrame1, MyDialog1
class MyFrame2(MyFrame1):
def __init__(self, parent):
MyFrame1.__init__ (self, parent)
def On_child_button( self, event ):
MyDialog2(None).Show()
class MyDialog2(MyDialog1):
def __init__(self, parent):
MyDialog1.__init__ (self, parent)
def onClose(self, event):
self.Destroy()
app = wx.App(0)
MyFrame2(None).Show()
app.MainLoop()
main.py
import wx
# ************ FRAME 1 ************ #
# ************ FRAME 1 ************ #
class MyFrame1 ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
bSizer = wx.BoxSizer( wx.VERTICAL )
self.child_button = wx.Button( self, wx.ID_ANY, u"Child Frame", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer.Add( self.child_button, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )
self.SetSizer( bSizer )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.child_button.Bind( wx.EVT_BUTTON, self.On_child_button )
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def On_child_button( self, event ):
event.Skip()
# ************ DIALOG 1 ************ #
# ************ DIALOG 1 ************ #
class MyDialog1 ( wx.Dialog ):
def __init__( self, parent ):
wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 300,200 ), style = wx.DEFAULT_DIALOG_STYLE )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
self.Centre( wx.BOTH )
# Connect Events
self.Bind( wx.EVT_CLOSE, self.onClose )
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def onClose( self, event ):
event.Skip()
import wx
from frame import MyFrame1, MyDialog1
class MyFrame2(MyFrame1):
def __init__(self, parent):
MyFrame1.__init__ (self, parent)
def On_child_button( self, event ):
MyDialog2(None).Show()
class MyDialog2(MyDialog1):
def __init__(self, parent):
MyDialog1.__init__ (self, parent)
def onClose(self, event):
self.Destroy()
app = wx.App(0)
MyFrame2(None).Show()
app.MainLoop()
您不需要在框架上使用
MakeModal
,因为对话框已经内置了模态功能。只需调用对话框的showmodel
方法,而不是Show
方法ShowModal
将创建一个嵌套的事件循环,因此在对话框完成之前,ShowModal
不会返回,并且应用程序中其他窗口的输入将被阻止。典型的使用模式如下所示:
with MyDialog(self, foo, bar) as dlg:
if dlg.ShowModal() == wx.ID_OK:
# do something with dlg values
改为尝试ShowModal()。