如何使用 wxpython 将 2 个框架放入 1 个应用程序中?
你好,我创建了 2 个框架,当我运行这个程序时,它会将每个框架显示为它们自己的应用程序(至少在 Windows 上)。有没有一种方法可以同时使用这两个框架,但将它们放在一个应用程序中?
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.NewId(), "Main")
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.button = wx.Button(self, wx.NewId(), "Open a child")
self.sizer.Add(self.button, proportion=0, border=2, flag=wx.ALL)
self.SetSizer(self.sizer)
self.button.Bind(wx.EVT_BUTTON, self.on_button)
self.Layout()
def on_button(self, evt):
frame = ChildFrame(self)
frame.Show(True)
frame.MakeModal(True)
class ChildFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, wx.NewId(), "Child")
self.Bind(wx.EVT_CLOSE, self.on_close)
def on_close(self, evt):
self.MakeModal(False)
evt.Skip()
class MyApp(wx.App):
def OnInit(self):
frame = MainFrame()
frame.Show(True)
self.SetTopWindow(frame)
return True
app = MyApp(0)
app.MainLoop()
这是我不想要的图像: https://i.sstatic.net/7gayc.png
这是我不想要的, 我希望两个框架都在一个应用程序中。
Hello i have created 2 frames, and when I run this program it will show each frame as their own application (at least on windows). Is there a way to use both frames but put them in one application?
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.NewId(), "Main")
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.button = wx.Button(self, wx.NewId(), "Open a child")
self.sizer.Add(self.button, proportion=0, border=2, flag=wx.ALL)
self.SetSizer(self.sizer)
self.button.Bind(wx.EVT_BUTTON, self.on_button)
self.Layout()
def on_button(self, evt):
frame = ChildFrame(self)
frame.Show(True)
frame.MakeModal(True)
class ChildFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, wx.NewId(), "Child")
self.Bind(wx.EVT_CLOSE, self.on_close)
def on_close(self, evt):
self.MakeModal(False)
evt.Skip()
class MyApp(wx.App):
def OnInit(self):
frame = MainFrame()
frame.Show(True)
self.SetTopWindow(frame)
return True
app = MyApp(0)
app.MainLoop()
Here is an image of what I do not want:
https://i.sstatic.net/7gayc.png
that is what I do not want,
I would like both frames to be in ONE application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些框架已经在同一个 wxPython 应用程序 (
MyApp
) 中运行。可能您的意思是在同一个 wxPython 窗口中拥有两个框架/窗口。
我认为这是不可能的,因为
wx.Frame
类根据定义是一个独立的窗口。您可以非常轻松地修改当前的类以从
wx.Panel
派生,而不是从wx.Frame
派生,然后将两个面板放在同一个 Common Frame 中。例如,对于您修改的 ChildFrame,如下所示:
然后您创建通用框架并实例化您的面板(并将它们设置在 sizer 中):
如果按照 Inerdial 的评论中的建议,您实际上正在寻找 MDI 窗口:
然后您必须使用 wx.MDIParentFrame 和 wx.MDIChildFrame 的类。有关示例,请查看 wxPython 文档和演示 包中的 MDIDemo.py 示例。请注意,虽然 wxPython 支持多文档界面窗口,但 Windows 已弃用 MDI。
These frames already are running in the same wxPython application (
MyApp
).Probably what you mean is to have the two frames/windows in the same wxPython window.
I think this is not possible because
wx.Frame
class is by definition an independent window.What you can do very easily is to modify your current classes to derive from
wx.Panel
instead of fromwx.Frame
and then put the two panels in the same Common Frame.For example for the ChildFrame you modify like this:
Then you create the common Frame and instatiate your panels (and set them in a sizer):
If as suggested in Inerdial's comment you are actually looking for MDI windows:
Then you have to use the wx.MDIParentFrame and wx.MDIChildFrame's classes. For an example, check the MDIDemo.py example in the wxPython docs and demos package. Note that although wxPython supports Multiple Document Inteface windows, Windows has deprecated MDI.
我认为在初始化子框架时将子框架的样式选项作为 wx.FRAME_NO_TASKBAR 放在参数中可能会解决问题。
I think putting the style option of the child frame as wx.FRAME_NO_TASKBAR in the argument while initializing child frame may solve the issue.