Python - wxPython 中的多态性,出了什么问题?

标签 python wxpython polymorphism pydev

我正在尝试在 wx.Python 中编写一个简单的自定义按钮。我的代码如下,我的“Custom_Button.py”第 19 行抛出错误 - 发生了什么事?我在网上找不到这个错误的帮助,并怀疑它与多态性有关。 (顺便说一句:我对 python 比较陌生,因为我之前学过 C++ 和 C#,任何有关代码语法和功能的帮助都会很棒! - 知道这一点,这可能是一个简单的错误。谢谢!)

错误

def __init__(self, parent, id=-1, NORM_BMP, PUSH_BMP, MOUSE_OVER_BMP, **kwargs):
      SyntaxError: non-default argument follows default argument

主.py
class MyFrame(wx.Frame):
    def __init__(self, parent, ID, title):
        wxFrame.__init__(self, parent, ID, title,
                         wxDefaultPosition, wxSize(400, 400))

        self.CreateStatusBar()
        self.SetStatusText("Program testing custom button overlays")
        menu = wxMenu()
        menu.Append(ID_ABOUT, "&About", "More information about this program")
        menu.AppendSeparator()
        menu.Append(ID_EXIT, "E&xit", "Terminate the program")
        menuBar = wxMenuBar()
        menuBar.Append(menu, "&File");
        self.SetMenuBar(menuBar)

        self.Button1 = Custom_Button(self, parent, -1, 
                                "D:/Documents/Python/Normal.bmp", 
                                "D:/Documents/Python/Clicked.bmp",
                                "D:/Documents/Python/Over.bmp",
                                "None", wx.Point(200,200), wx.Size(300,100))

        EVT_MENU(self, ID_ABOUT, self.OnAbout)
        EVT_MENU(self, ID_EXIT,  self.TimeToQuit)

    def OnAbout(self, event):
        dlg = wxMessageDialog(self, "Testing the functions of custom "
                              "buttons using pyDev and wxPython",
                              "About", wxOK | wxICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()


    def TimeToQuit(self, event):
        self.Close(true)



class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(NULL, -1, "wxPython | Buttons")
        frame.Show(true)
        self.SetTopWindow(frame)
        return true

app = MyApp(0)
app.MainLoop()

自定义按钮

import wx
from wxPython.wx import *

class Custom_Button(wx.PyControl):
                                    ############################################
                                  ##THE ERROR IS BEING THROWN SOME WHERE IN HERE ##
                                    ############################################
                                    # The BMP's
    Mouse_over_bmp = wx.Bitmap(0)   # When the mouse is over
    Norm_bmp = wx.Bitmap(0)         # The normal BMP
    Push_bmp = wx.Bitmap(0)         # The down BMP

    Pos_bmp = wx.Point(0,0)         # The posisition of the button

    def __init__(self, parent, NORM_BMP, PUSH_BMP, MOUSE_OVER_BMP, text="", 
                pos, size, id=-1, **kwargs):
        wx.PyControl.__init__(self,parent, id, **kwargs)

        # Set the BMP's to the ones given in the constructor
        self.Mouse_over_bmp = wx.Bitmap(MOUSE_OVER_BMP)
        self.Norm_bmp = wx.Bitmap(NORM_BMP)
        self.Push_bmp = wx.Bitmap(PUSH_BMP)
        self.Pos_bmp = pos

                                    ############################################
                                  ##THE ERROR IS BEING THROWN SOME WHERE IN HERE ##
                                    ############################################

        self.Bind(wx.EVT_LEFT_DOWN, self._onMouseDown)
        self.Bind(wx.EVT_LEFT_UP, self._onMouseUp)
        self.Bind(wx.EVT_LEAVE_WINDOW, self._onMouseLeave)
        self.Bind(wx.EVT_ENTER_WINDOW, self._onMouseEnter)
        self.Bind(wx.EVT_ERASE_BACKGROUND,self._onEraseBackground)
        self.Bind(wx.EVT_PAINT,self._onPaint)

        self._mouseIn = self._mouseDown = False

    def _onMouseEnter(self, event):
        self._mouseIn = True

    def _onMouseLeave(self, event):
        self._mouseIn = False

    def _onMouseDown(self, event):
        self._mouseDown = True

    def _onMouseUp(self, event):
        self._mouseDown = False
        self.sendButtonEvent()

    def sendButtonEvent(self):
        event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, self.GetId())
        event.SetInt(0)
        event.SetEventObject(self)
        self.GetEventHandler().ProcessEvent(event)

    def _onEraseBackground(self,event):
        # reduce flicker
        pass

    def _onPaint(self, event):
        dc = wx.BufferedPaintDC(self)
        dc.SetFont(self.GetFont())
        dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
        dc.Clear()
        dc.DrawBitmap(self.Norm_bmp)

        # draw whatever you want to draw
        # draw glossy bitmaps e.g. dc.DrawBitmap
        if self._mouseIn:   # If the Mouse is over the button
            dc.DrawBitmap(self, self.Mouse_over_bmp, self.Pos_bmp, useMask=False)
        if self._mouseDown: # If the Mouse clicks the button
            dc.DrawBitmap(self, self.Push_bmp, self.Pos_bmp, useMask=False)

最佳答案

在函数定义中,具有默认值的参数需要列在没有默认值的参数之后,但在 *args 和 **kwargs 扩展之前

之前:

def __init__(self, parent, id=-1, NORM_BMP, PUSH_BMP, MOUSE_OVER_BMP, text="", 
                pos, size, **kwargs)

更正:

def __init__(self, parent, NORM_BMP, PUSH_BMP, MOUSE_OVER_BMP, 
                pos, size, id=-1, text="", **kwargs)

关于Python - wxPython 中的多态性,出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2990446/

相关文章:

Python 交互式循环中断

python - wxPython - DropSource.SetData() 适用于字符串,但不适用于列表

c++ - 将方法参数类型更改为子类中的派生类

具有复合/多参数类型构造函数的 Haskell 类型签名

Java 实例与抽象类和方法

Python Selenium Webdriver - 在指定的一个之后抓取 div

python - Django URL//双斜杠被删除(可能是 Apache 的错)?

python - 打包应用程序时如何排除不必要的Qt *.so 文件?

python - Python 和 wxPython 可以在 Windows 98 上运行吗?

wxpython - wx.Panel缩放以适合整个父框架,尽管为其设置了尺寸