【问题标题】:python error: method takes exactly 1 argument (2 given)python错误:方法只需要1个参数(给定2个)
【发布时间】:2012-04-25 10:48:54
【问题描述】:

我的 python 程序有一个小问题,我在 wxpython 中工作,我试图通过一个按钮以读取模式打开一个管道,该按钮通过事件绑定到函数“checkpipe”。问题是当我单击按钮时,python 错误解释器告诉我:

class fenetre(wx.Frame): #on va creer une nouvelle classe pour l'interface
    def __init__(self, parent, id): #constructeur
        filecontent = ""
        wx.Frame.__init__(self, parent, id, 'Solution 1 ---> Destinataire', size = (640,480))
        panel = wx.Panel(self)
        verifybutton = wx.Button(panel,label = "Verifier", pos = (320,10), size = (80,30))
        self.currentDirectory = os.getcwd() #trouver le chemin du dossier dans lequel on se trouve
        self.Centre() #faire apparaitre la fenetre au centre
        textprocessid = wx.StaticText(panel, -1, monid, pos = (400, 213))
        self.Bind(wx.EVT_BUTTON, self.checkpipe, verifybutton)


    def alertMessagePipeEmpty(self):
        dialog = wx.MessageDialog(self, "Rien recu. Veuillez essayer plus tard","Erreur", wx.OK|wx.ICON_ERROR)
        result = dialog.ShowModal()
        dialog.Destroy()

    def closewindow(self, event):
        self.Destroy()

    def checkpipe(self,names = None):
        if not os.path.exists(fifoname):
            os.mkfifo(fifoname)
        pipein = open(fifoname, 'r') 
        contenu = pipein.read()
        if contenu == "":
            self.alertMessagePipeEmpty()
        else:
            dialog = wx.MessageDialog(self, "Vous avez un fichier a recevoir. Voulez-vous le sauvegarder","Fichier recu", wx.YES_DEFAULT|wx.ICON_ERROR)
            result = dialog.ShowModal()
            dialog.Destroy()
            if result == wx.ID_YES:
                dlg = wx.FileDialog(
                self, message="Sauvegardez le fichier",
                defaultDir=self.currentDirectory,
                defaultFile="",
                wildcard=wildcard,
                style=wx.SAVE | wx.CHANGE_DIR
                )
                if dlg.ShowModal() == wx.ID_OK:
                    paths = dlg.GetPaths()
                    pathcomplet = ""
                    for path in paths:
                        pathcomplet = pathcomplet + path
                dlg.Destroy()
                fd2 = open(pathcomplet,'w')
                fd2.write(contenu)
                os.unlink(fifoname)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = fenetre(parent = None, id = -1)
    frame.Show()
    app.MainLoop()

TypeError: checkpipe() 只接受 1 个参数(给定 2 个)

我尝试了所有方法,我想这是关于我错过的一些愚蠢的事情,但我似乎无法弄清楚。任何帮助将不胜感激。

【问题讨论】:

  • 你能贴一点代码吗?可能是调用者代码。
  • 你能贴一些代码吗?
  • 疯狂猜测:一个函数被称为类方法而不是静态方法。

标签: python methods arguments


【解决方案1】:

wx 中处理事件的方法会自动将事件作为参数传递。

您应该将检查管道重写为: def checkpipe(self, event, names=None)

【讨论】:

  • 我不知道 wxWidgets,但我非常确信这就是答案。在我使用过的大多数框架中都是这种情况:回调总是接收事件参数。
猜你喜欢
  • 1970-01-01
  • 2018-03-29
  • 1970-01-01
  • 2010-11-11
  • 2012-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多