wxpython演示实现

时间:2012-05-01 20:05:16

标签: python wxpython demo

我是python的初学者,但我想使用Python的“Button Panel”演示代码编写一个简单的游戏。但是我不确定如何从演示应用程序中取出代码并在我自己的应用程序中使用它。具体来说,以下几行代码,我不知道如何替换。我意识到演示称为“父母”,我不知道该说什么代替“父母。(某事)”:

class SettingsPanel(wx.MiniFrame):

    def __init__(self, parent, id=wx.ID_ANY, title="Settings Panel", pos=wx.DefaultPosition,
             size=wx.DefaultSize,
             style=wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.FRAME_NO_TASKBAR
             | wx.FRAME_FLOAT_ON_PARENT | wx.CLIP_CHILDREN):

        wx.MiniFrame.__init__(self, parent, id, title, pos, size, style)

        self.targetTitleBar = parent.titleBar
        self.parent = parent
        self.panel = wx.Panel(self, -1)
    self.coloursizer_staticbox = wx.StaticBox(self.panel, -1, "Colour Options")
    self.bottomsizer_staticbox = wx.StaticBox(self.panel, -1, "Size Options")
    self.stylesizer_staticbox = wx.StaticBox(self.panel, -1, "ButtonPanel Styles")
    self.defaultstyle = wx.RadioButton(self.panel, -1, "Default Style", style=wx.RB_GROUP)
    self.gradientstyle = wx.RadioButton(self.panel, -1, "Gradient Style")
    self.verticalgradient = wx.RadioButton(self.panel, -1, "Vertical Gradient", style=wx.RB_GROUP)
    self.horizontalgradient = wx.RadioButton(self.panel, -1, "Horizontal Gradient")

    b = self.CreateColourBitmap(wx.BLACK)

    self.bakbrush = wx.BitmapButton(self.panel, ID_BackgroundColour, b, size=wx.Size(50,25))
    self.gradientfrom = wx.BitmapButton(self.panel, ID_GradientFrom, b, size=wx.Size(50,25))
    self.gradientto = wx.BitmapButton(self.panel, ID_GradientTo, b, size=wx.Size(50,25))
    self.bordercolour = wx.BitmapButton(self.panel, ID_BorderColour, b, size=wx.Size(50,25))
    self.captioncolour = wx.BitmapButton(self.panel, ID_CaptionColour, b, size=wx.Size(50,25))
    self.textbuttoncolour = wx.BitmapButton(self.panel, ID_ButtonTextColour, b, size=wx.Size(50,25))
    self.selectionbrush = wx.BitmapButton(self.panel, ID_SelectionBrush, b, size=wx.Size(50,25))
    self.selectionpen = wx.BitmapButton(self.panel, ID_SelectionPen, b, size=wx.Size(50,25))
    self.separatorcolour = wx.BitmapButton(self.panel, ID_SeparatorColour, b, size=wx.Size(50,25))

    self.separatorspin = wx.SpinCtrl(self.panel, -1, "7", min=3, max=20,
                                     style=wx.SP_ARROW_KEYS)
    self.marginspin = wx.SpinCtrl(self.panel, -1, "6", min=3, max=20,
                                  style=wx.SP_ARROW_KEYS)
    self.paddingspin = wx.SpinCtrl(self.panel, -1, "6", min=3, max=20,
                                   style=wx.SP_ARROW_KEYS)
    self.borderspin = wx.SpinCtrl(self.panel, -1, "3", min=3, max=7,
                                  style=wx.SP_ARROW_KEYS)

    self.__set_properties()
    self.__do_layout()

    self.Bind(wx.EVT_RADIOBUTTON, self.OnDefaultStyle, self.defaultstyle)
    self.Bind(wx.EVT_RADIOBUTTON, self.OnGradientStyle, self.gradientstyle)
    self.Bind(wx.EVT_RADIOBUTTON, self.OnVerticalGradient, self.verticalgradient)
    self.Bind(wx.EVT_RADIOBUTTON, self.OnHorizontalGradient, self.horizontalgradient)
    self.Bind(wx.EVT_BUTTON, self.OnSetColour, id=ID_BackgroundColour)
    self.Bind(wx.EVT_BUTTON, self.OnSetColour, id=ID_GradientFrom)
    self.Bind(wx.EVT_BUTTON, self.OnSetColour, id=ID_GradientTo)
    self.Bind(wx.EVT_BUTTON, self.OnSetColour, id=ID_BorderColour)
    self.Bind(wx.EVT_BUTTON, self.OnSetColour, id=ID_CaptionColour)
    self.Bind(wx.EVT_BUTTON, self.OnSetColour, id=ID_ButtonTextColour)
    self.Bind(wx.EVT_BUTTON, self.OnSetColour, id=ID_SelectionBrush)
    self.Bind(wx.EVT_BUTTON, self.OnSetColour, id=ID_SelectionPen)
    self.Bind(wx.EVT_BUTTON, self.OnSetColour, id=ID_SeparatorColour)

    self.Bind(wx.EVT_SPINCTRL, self.OnSeparator, self.separatorspin)
    self.Bind(wx.EVT_SPINCTRL, self.OnMargins, self.marginspin)
    self.Bind(wx.EVT_SPINCTRL, self.OnPadding, self.paddingspin)
    self.Bind(wx.EVT_SPINCTRL, self.OnBorder, self.borderspin)

    self.Bind(wx.EVT_CLOSE, self.OnClose)        


def __set_properties(self):

    self.defaultstyle.SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
    self.defaultstyle.SetValue(1)
    self.gradientstyle.SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
    self.verticalgradient.SetValue(1)

    if self.targetTitleBar.GetStyle() & bp.BP_DEFAULT_STYLE:
        self.verticalgradient.Enable(False)
        self.horizontalgradient.Enable(False)
        self.defaultstyle.SetValue(1)
    else:
        self.gradientstyle.SetValue(1)

    self.borderspin.SetValue(self.targetTitleBar.GetBPArt().GetMetric(bp.BP_BORDER_SIZE))
    self.separatorspin.SetValue(self.targetTitleBar.GetBPArt().GetMetric(bp.BP_SEPARATOR_SIZE))
    self.marginspin.SetValue(self.targetTitleBar.GetBPArt().GetMetric(bp.BP_MARGINS_SIZE).x)
    self.paddingspin.SetValue(self.targetTitleBar.GetBPArt().GetMetric(bp.BP_PADDING_SIZE).x)

    self.UpdateColours()        


def __do_layout(self):

    mainsizer = wx.BoxSizer(wx.VERTICAL)
    buttonsizer = wx.BoxSizer(wx.HORIZONTAL)
    bottomsizer = wx.StaticBoxSizer(self.bottomsizer_staticbox, wx.VERTICAL)
    sizer_13 = wx.BoxSizer(wx.HORIZONTAL)
    sizer_12 = wx.BoxSizer(wx.HORIZONTAL)
    sizer_11 = wx.BoxSizer(wx.HORIZONTAL)
    sizer_10 = wx.BoxSizer(wx.HORIZONTAL)
    coloursizer = wx.StaticBoxSizer(self.coloursizer_staticbox, wx.HORIZONTAL)
    rightsizer = wx.BoxSizer(wx.VERTICAL)
    sizer_9 = wx.BoxSizer(wx.HORIZONTAL)
    sizer_8 = wx.BoxSizer(wx.HORIZONTAL)
    sizer_7 = wx.BoxSizer(wx.HORIZONTAL)
    sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
    leftsizer = wx.BoxSizer(wx.VERTICAL)
    sizer_5 = wx.BoxSizer(wx.HORIZONTAL)
    sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
    sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
    sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
    sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
    stylesizer = wx.StaticBoxSizer(self.stylesizer_staticbox, wx.VERTICAL)
    tophsizer = wx.BoxSizer(wx.HORIZONTAL)
    tophsizer2 = wx.BoxSizer(wx.VERTICAL)

    stylesizer.Add(self.defaultstyle, 0, wx.ALL|wx.EXPAND|wx.ADJUST_MINSIZE, 5)

    tophsizer.Add(self.gradientstyle, 0, wx.LEFT|wx.RIGHT|wx.EXPAND|
                  wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)

    tophsizer2.Add(self.verticalgradient, 0, wx.BOTTOM|wx.ADJUST_MINSIZE, 3)
    tophsizer2.Add(self.horizontalgradient, 0, wx.ADJUST_MINSIZE, 0)

    tophsizer.Add(tophsizer2, 1, wx.LEFT|wx.EXPAND|wx.ALIGN_CENTER_VERTICAL, 10)

    stylesizer.Add(tophsizer, 1, wx.EXPAND, 0)

    mainsizer.Add(stylesizer, 0, wx.ALL|wx.EXPAND, 5)

    label_1 = wx.StaticText(self.panel, -1, "Background Brush Colour:")
    sizer_1.Add(label_1, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
    sizer_1.Add((0, 0), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
    sizer_1.Add(self.bakbrush, 0, wx.ADJUST_MINSIZE, 0)

    leftsizer.Add(sizer_1, 1, wx.EXPAND, 0)

    label_2 = wx.StaticText(self.panel, -1, "Gradient Colour From:")
    sizer_2.Add(label_2, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
    sizer_2.Add((0, 0), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
    sizer_2.Add(self.gradientfrom, 0, wx.ADJUST_MINSIZE, 0)

    leftsizer.Add(sizer_2, 1, wx.EXPAND, 0)

    label_3 = wx.StaticText(self.panel, -1, "Gradient Colour To:")
    sizer_3.Add(label_3, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
    sizer_3.Add((0, 0), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
    sizer_3.Add(self.gradientto, 0, wx.ADJUST_MINSIZE, 0)

    leftsizer.Add(sizer_3, 1, wx.EXPAND, 0)

    label_4 = wx.StaticText(self.panel, -1, "Border Colour:")
    sizer_4.Add(label_4, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
    sizer_4.Add((0, 0), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
    sizer_4.Add(self.bordercolour, 0, wx.ADJUST_MINSIZE, 0)

    leftsizer.Add(sizer_4, 1, wx.EXPAND, 0)

    label_5 = wx.StaticText(self.panel, -1, "Main Caption Colour:")
    sizer_5.Add(label_5, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
    sizer_5.Add((0, 0), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
    sizer_5.Add(self.captioncolour, 0, wx.ADJUST_MINSIZE, 0)

    leftsizer.Add(sizer_5, 1, wx.EXPAND, 0)

    coloursizer.Add(leftsizer, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
    coloursizer.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)

    label_6 = wx.StaticText(self.panel, -1, "Text Button Colour:")
    sizer_6.Add(label_6, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
    sizer_6.Add((0, 0), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
    sizer_6.Add(self.textbuttoncolour, 0, wx.ADJUST_MINSIZE, 0)

    rightsizer.Add(sizer_6, 1, wx.EXPAND, 0)

    label_7 = wx.StaticText(self.panel, -1, "Selection Brush Colour:")
    sizer_7.Add(label_7, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
    sizer_7.Add((0, 0), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
    sizer_7.Add(self.selectionbrush, 0, wx.ADJUST_MINSIZE, 0)

    rightsizer.Add(sizer_7, 1, wx.EXPAND, 0)

    label_8 = wx.StaticText(self.panel, -1, "Selection Pen Colour:")
    sizer_8.Add(label_8, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
    sizer_8.Add((0, 0), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
    sizer_8.Add(self.selectionpen, 0, wx.ADJUST_MINSIZE, 0)

    rightsizer.Add(sizer_8, 1, wx.EXPAND, 0)

    label_9 = wx.StaticText(self.panel, -1, "Separator Colour:")
    sizer_9.Add(label_9, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
    sizer_9.Add((0, 0), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
    sizer_9.Add(self.separatorcolour, 0, wx.ADJUST_MINSIZE, 0)

    rightsizer.Add(sizer_9, 1, wx.EXPAND, 0)

    coloursizer.Add(rightsizer, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)

    mainsizer.Add(coloursizer, 0, wx.ALL|wx.EXPAND, 5)

    label_10 = wx.StaticText(self.panel, -1, "Separator Size:")
    sizer_10.Add(label_10, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
    sizer_10.Add((0, 0), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
    sizer_10.Add(self.separatorspin, 0, wx.ALL|wx.ADJUST_MINSIZE, 5)

    bottomsizer.Add(sizer_10, 1, wx.EXPAND, 0)

    label_11 = wx.StaticText(self.panel, -1, "Margins Size:")
    sizer_11.Add(label_11, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
    sizer_11.Add((0, 0), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
    sizer_11.Add(self.marginspin, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ADJUST_MINSIZE, 5)

    bottomsizer.Add(sizer_11, 1, wx.EXPAND, 0)

    label_12 = wx.StaticText(self.panel, -1, "Padding Size:")
    sizer_12.Add(label_12, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
    sizer_12.Add((0, 0), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
    sizer_12.Add(self.paddingspin, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ADJUST_MINSIZE, 5)

    bottomsizer.Add(sizer_12, 1, wx.EXPAND, 0)

    label_13 = wx.StaticText(self.panel, -1, "Border Size:")
    sizer_13.Add(label_13, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
    sizer_13.Add((0, 0), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
    sizer_13.Add(self.borderspin, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ADJUST_MINSIZE, 5)

    bottomsizer.Add(sizer_13, 1, wx.EXPAND, 0)

    mainsizer.Add(bottomsizer, 0, wx.ALL|wx.EXPAND, 5)

    self.panel.SetSizer(mainsizer)
    sizer = wx.BoxSizer()
    sizer.Add(self.panel, 1, wx.EXPAND)
    self.SetSizer(sizer)
    self.Fit()


def CreateColourBitmap(self, c):

    image = wx.EmptyImage(25, 14)

    for x in xrange(25):
        for y in xrange(14):
            pixcol = c
            if x == 0 or x == 24 or y == 0 or y == 13:
                pixcol = wx.BLACK

            image.SetRGB(x, y, pixcol.Red(), pixcol.Green(), pixcol.Blue())

    return image.ConvertToBitmap()


def UpdateColours(self):

    bk = self.targetTitleBar.GetBPArt().GetColour(bp.BP_BACKGROUND_COLOUR)
    self.bakbrush.SetBitmapLabel(self.CreateColourBitmap(bk))

    capfrom = self.targetTitleBar.GetBPArt().GetColour(bp.BP_GRADIENT_COLOUR_FROM)
    self.gradientfrom.SetBitmapLabel(self.CreateColourBitmap(capfrom))

    capto = self.targetTitleBar.GetBPArt().GetColour(bp.BP_GRADIENT_COLOUR_TO)
    self.gradientto.SetBitmapLabel(self.CreateColourBitmap(capto))

    captxt = self.targetTitleBar.GetBPArt().GetColour(bp.BP_TEXT_COLOUR)
    self.captioncolour.SetBitmapLabel(self.CreateColourBitmap(captxt))

    bor = self.targetTitleBar.GetBPArt().GetColour(bp.BP_BORDER_COLOUR)
    self.bordercolour.SetBitmapLabel(self.CreateColourBitmap(bor))

    btntext = self.targetTitleBar.GetBPArt().GetColour(bp.BP_BUTTONTEXT_COLOUR)
    self.textbuttoncolour.SetBitmapLabel(self.CreateColourBitmap(btntext))

    selb = self.targetTitleBar.GetBPArt().GetColour(bp.BP_SELECTION_BRUSH_COLOUR)
    self.selectionbrush.SetBitmapLabel(self.CreateColourBitmap(selb))

    selp = self.targetTitleBar.GetBPArt().GetColour(bp.BP_SELECTION_PEN_COLOUR)
    self.selectionpen.SetBitmapLabel(self.CreateColourBitmap(selp))

    sepc = self.targetTitleBar.GetBPArt().GetColour(bp.BP_SEPARATOR_COLOUR)
    self.separatorcolour.SetBitmapLabel(self.CreateColourBitmap(sepc))


def OnDefaultStyle(self, event):

    self.verticalgradient.Enable(False)
    self.horizontalgradient.Enable(False)
    self.targetTitleBar.SetStyle(bp.BP_DEFAULT_STYLE)

    self.targetTitleBar.Refresh()

    event.Skip()


def OnGradientStyle(self, event): 

    self.verticalgradient.Enable(True)
    self.horizontalgradient.Enable(True)
    self.targetTitleBar.SetStyle(bp.BP_USE_GRADIENT)
    self.targetTitleBar.Refresh()

    event.Skip()


def OnVerticalGradient(self, event):

    self.targetTitleBar.GetBPArt().SetGradientType(bp.BP_GRADIENT_VERTICAL)
    self.targetTitleBar.SetStyle(bp.BP_USE_GRADIENT)
    self.targetTitleBar.Refresh()

    event.Skip()


def OnHorizontalGradient(self, event):

    self.targetTitleBar.GetBPArt().SetGradientType(bp.BP_GRADIENT_HORIZONTAL)
    self.targetTitleBar.SetStyle(bp.BP_USE_GRADIENT)
    self.targetTitleBar.Refresh()

    event.Skip()


def OnSetColour(self, event):

    dlg = wx.ColourDialog(self.parent)

    dlg.SetTitle("Colour Picker")

    if dlg.ShowModal() != wx.ID_OK:
        return

    var = 0
    if event.GetId() == ID_BackgroundColour:
        var = bp.BP_BACKGROUND_COLOUR
    elif event.GetId() == ID_GradientFrom:
        var = bp.BP_GRADIENT_COLOUR_FROM
    elif event.GetId() == ID_GradientTo:
        var = bp.BP_GRADIENT_COLOUR_TO
    elif event.GetId() == ID_BorderColour:
        var = bp.BP_BORDER_COLOUR
    elif event.GetId() == ID_CaptionColour:
        var = bp.BP_TEXT_COLOUR
    elif event.GetId() == ID_ButtonTextColour:
        var = bp.BP_BUTTONTEXT_COLOUR
    elif event.GetId() == ID_SelectionBrush:
        var = bp.BP_SELECTION_BRUSH_COLOUR
    elif event.GetId() == ID_SelectionPen:
        var = bp.BP_SELECTION_PEN_COLOUR
    elif event.GetId() == ID_SeparatorColour:
        var = bp.BP_SEPARATOR_COLOUR
    else:
        return        

    self.targetTitleBar.GetBPArt().SetColour(var, dlg.GetColourData().GetColour())
    self.targetTitleBar.Refresh()
    self.UpdateColours()

    self.parent.useredited = True


def OnSeparator(self, event):

    self.targetTitleBar.GetBPArt().SetMetric(bp.BP_SEPARATOR_SIZE,
                                             event.GetInt())

    self.targetTitleBar.DoLayout()
    self.parent.mainPanel.Layout()
    self.parent.useredited = True

    event.Skip()


def OnMargins(self, event):

    self.targetTitleBar.GetBPArt().SetMetric(bp.BP_MARGINS_SIZE,
                                             wx.Size(event.GetInt(), event.GetInt()))

    self.targetTitleBar.DoLayout()
    self.parent.mainPanel.Layout()

    self.parent.useredited = True

    event.Skip()


def OnPadding(self, event):

    self.targetTitleBar.GetBPArt().SetMetric(bp.BP_PADDING_SIZE,
                                             wx.Size(event.GetInt(), event.GetInt()))

    self.targetTitleBar.DoLayout()
    self.parent.mainPanel.Layout()
    self.parent.useredited = True

    event.Skip()


def OnBorder(self, event):

    self.targetTitleBar.GetBPArt().SetMetric(bp.BP_BORDER_SIZE,
                                             event.GetInt())

    self.targetTitleBar.DoLayout()
    self.parent.mainPanel.Layout()

    self.parent.useredited = True

    event.Skip()


def OnClose(self, event):

    self.parent.hassettingpanel = False
    self.Destroy()enter code here

例如,我会用什么代替“parent.titleBar”?我是否需要更改代码中的任何其他内容才能使其独立运行?

2 个答案:

答案 0 :(得分:0)

您通常必须从演示中调整代码,以便它可以在该框架之外运行。不久前我在这个主题上写了一个wiki页面here

父母很可能是一个wx.Frame。几乎所有小部件都需要父级。就个人而言,如果我要用Python编写游戏,我会使用PyGame或Pyglet或类似的东西。

答案 1 :(得分:0)

将小部件从演示环境移动到应用程序通常需要进行一些更改。通常这涉及创建一个App(在下面的示例中我使用PySimpleApp),并删除日志。以下是一个有效的完整示例:

import wx 
class SettingsPanel(wx.MiniFrame):

    def __init__(self, parent, id=wx.ID_ANY, title="Settings Panel", pos=wx.DefaultPosition,
             size=wx.DefaultSize,
             style=wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.FRAME_NO_TASKBAR
             | wx.FRAME_FLOAT_ON_PARENT | wx.CLIP_CHILDREN):

        wx.MiniFrame.__init__(self, parent, id, title, pos, size, style)

        #self.targetTitleBar = parent.titleBar
        self.parent = parent
        self.panel = wx.Panel(self, -1)

app = wx.PySimpleApp() 
frame = SettingsPanel(None)
frame.Show()
app.MainLoop()

帧不需要父级,所以这里我只传入None作为父级,因为None没有titleBar属性,所以我注释掉了这一行。

更新:如果你想用titleBar运行它,但又不想使用真实的titleBar对象,你可以创建一个模拟对象。如果您愿意,有几个模拟库可以帮助您,但由于您的titleBar只有一些方法,因此最简单的方法就是自己创建一个模拟对象。您需要添加所有调用的方法,但我创建了一个只有Refresh方法的简单版本。此外,由于您现在无法使用parent=None,我添加了一个框架作为迷你框架的父级。

import wx 

class SettingsPanel(wx.MiniFrame):

    def __init__(self, parent, id=wx.ID_ANY, title="Settings Panel", pos=wx.DefaultPosition,
             size=wx.DefaultSize,
             style=wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.FRAME_NO_TASKBAR
             | wx.FRAME_FLOAT_ON_PARENT | wx.CLIP_CHILDREN):

        wx.MiniFrame.__init__(self, parent, id, title, pos, size, style)

        self.targetTitleBar = parent.titleBar
        self.parent = parent
        self.panel = wx.Panel(self, -1)

        self.targetTitleBar.Refresh() # an example of calling the mock object

class MockTitleBar(object):
    def __init__(self):
        pass
    def Refresh(self):
        print "refresh"

app = wx.PySimpleApp()
title_bar = MockTitleBar()
top_frame = wx.Frame(None)
top_frame.titleBar = title_bar
frame = SettingsPanel(top_frame)
top_frame.Show()
frame.Show()
app.MainLoop()