1

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

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”?我是否需要更改代码中的其他任何内容以使其独立运行?

4

2 回答 2

0

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

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

于 2012-05-01T20:26:42.563 回答
0

将小部件从演示上下文移动到应用程序通常需要进行一些更改。通常这涉及创建一个应用程序(在下面的示例中,我使用了 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()
于 2012-05-01T20:33:10.127 回答