我需要在点击按钮(在物理上与主窗口分开)的wxpython中创建一个额外的新窗口。我想在不关闭最后一个窗口的情况下这样做。
这是我到目前为止所做的:
class Prototype(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, None, size=(1240,705))
self.UI()
self.Centre()
self.Show()
def UI(self):
self.panel1 = wx.Panel(self, -1)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.panel1, 1, flag=wx.EXPAND)
b = wx.Button(self.panel1, label='second window', size=(180,100), pos=(650,25))
b.Bind(wx.EVT_BUTTON, self.OnB)
self.panel2 = wx.Panel(self, -1)
self.sizer.Add(self.panel2,1,flag=wx.EXPAND)
self.panel2.Hide()
self.panel2.SetSizer(self.vbox)
self.SetSizer(self.sizer)
def OnB(self, event):
self.panel2.Show()
self.sizer.Layout()
app = wx.App()
Prototype(None, title='')
app.MainLoop()
答案 0 :(得分:3)
这是一个有效的例子:
import wx
########################################################################
class SecondFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Second Frame")
panel = wx.Panel(self)
txt = wx.StaticText(panel, label="I'm the second frame!")
########################################################################
class Prototype(wx.Frame):
#----------------------------------------------------------------------
def __init__(self, parent, title):
wx.Frame.__init__(self, None, title="First Frame", size=(1240,705))
self.UI()
self.Centre()
self.Show()
#----------------------------------------------------------------------
def UI(self):
self.panel1 = wx.Panel(self, -1)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.panel1, 1, flag=wx.EXPAND)
b = wx.Button(self.panel1, label='second window', size=(180,100), pos=(650,25))
b.Bind(wx.EVT_BUTTON, self.OnB)
self.SetSizer(self.sizer)
#----------------------------------------------------------------------
def OnB(self, event):
frame = SecondFrame()
frame.Show()
#----------------------------------------------------------------------
app = wx.App(False)
Prototype(None, title='')
app.MainLoop()
我不建议混合sizer和绝对定位。你应该使用其中一个。除非您打算制作无法调整大小的帧,否则我建议使用sizer。
答案 1 :(得分:0)
只需在应用中创建一个新的wx.Frame
即可。在行按钮事件处理程序中添加一些内容:
self.second_window = wx.Frame(None)
self.second_window.Show()