TypeError: in method 'new_Dialog', expected argument 1 of type 'wxWindow *'

TypeError: in method 'new_Dialog', expected argument 1 of type 'wxWindow *'

我正在尝试构建一个使用 SSH 浏览远程位置文件的文件浏览器。我的 GUI 代码不断向我抛出一个或另一个错误,所以我什至无法测试 SSH 部分(不包括在下面的代码中)。我当前的错误似乎是我的 class 构造函数或我调用它的方式 [SSHFileDialog] 的问题。如果有人能指出我在这里出错的地方,我将不胜感激。我对 Python 的了解是自学的,我最近才开始使用 wxPython.

编写 GUI 代码

代码:

import wx,os

class SSHFileDialog(wx.Dialog): #, hostname = 'DefaultHost', username = 'DefaultUser', password = 'Password'

    def __init__(self, parent):
        super(SSHFileDialog, self).__init__(self, parent, -1, style = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
        # self.hostname = kwargs['hostname']
        # self.username = kwargs['username']
        # self.password = kwargs['password']
        hostname = "Host"
        username = "User"
        self.SetMinSize((512,512))
        self.Centre()
        self.SetTitle("Remote File Browser: Connection established to %s as %s"% (hostname,username))
        #print password
        self.InitUI()

    def InitUI(self):

        currentDir = os.getcwd() #For Testing
        fileAttr = [("Test","Test","Test","Test")] #Need to get file attributes from all files/folders in directory: Name, Type, Size, ModDate

        pnl = wx.Panel(self)
        vbox =  wx.BoxSizer(wx.VERTICAL)

        h_Dir_Box = wx.BoxSizer(wx.HORIZONTAL)
        st_Dir = wx.StaticText(self, label = currentDir,style=wx.ALIGN_LEFT)
        btn_Back = wx.Button(self,label = 'Back')
        h_Dir_Box.Add(st_Dir,flag=wx.ALL, border = 5)
        h_Dir_Box.Add(btn_Back)


        stbox = wx.StaticBox(pnl, wx.ID_ANY, "Directory Contents")
        stboxS = wx.StaticBoxSizer(stbox, orient = wx.HORIZONTAL)
        list = wx.ListCtrl(stbox, style = wx.LC_REPORT|wx.LC_VRULES|wx.LC_SINGLE_SEL)
        list.InsertColumn(0,'Filename',width = 175)
        list.InsertColumn(1,'Type', width = 100)
        list.InsertColumn(2,'Size', width = 75)
        list.InsertColumn(3,'Date Modified',wx.LIST_FORMAT_RIGHT, 90)

        for i in fileAttr:
            index = list.InsertStringItem(len(fileAttr)+10,i[0])
            list.SetStringItem(index,1,i[1])
            list.SetStringItem(index,2,i[2])
            list.SetStringItem(index,3,i[3])

        pnl.SetSizer(stboxS)


        h_Open_Box = wx.BoxSizer(wx.HORIZONTAL)
        btn_Open = wx.Button(self, label = 'Open')
        btn_Can = wx.Button(self, label = 'Cancel')
        h_Open_Box.Add(btn_Open)
        h_Open_Box.Add(btn_Can,flag = wx.LEFT,border=10)


        vbox.Add(h_Dir_Box, flag=wx.ALL|wx.EXPAND, border = 10)
        vbox.Add(pnl, proportion =1 , flag=wx.ALL|wx.EXPAND|wx.ALIGN_CENTER, border = 20)
        vbox.Add(h_Open_Box, flag = wx.ALIGN_RIGHT)

        self.SetSizer(vbox)

        btn_Open.Bind(wx.EVT_BUTTON, self.OnClose)
        btn_Can.Bind(wx.EVT_BUTTON, self.OnClose)

    def OnClose(self,e):
        self.Destroy()


class TestGui(wx.Frame):

    def __init__(self,*args,**kwargs):
        super(TestGui,self).__init__(*args,**kwargs)

        self.InitUI()

    def InitUI(self):

        menubar = wx.MenuBar()
        fileMenu = wx.Menu()
        openFileItem = fileMenu.Append(wx.ID_OPEN,'&Open')
        fileMenu.AppendSeparator()

        quitApp = fileMenu.Append(wx.ID_EXIT, "&Quit\tCtrl+Q")

        menubar.Append(fileMenu, '&File')

        self.SetMenuBar(menubar)

        self.Bind(wx.EVT_MENU,self.OnQuit,quitApp)
        self.Bind(wx.EVT_MENU,self.OnOpen,openFileItem)

        self.SetSize((500,500))
        self.SetTitle('File Manager example')
        self.Centre()
        self.Show(True)

    def OnQuit(self,e):
        self.Close()

    def OnOpen(self,e):
        args = {'hostname':'Host','username':'user','password':'password'}
        fileDialog = SSHFileDialog(None)
        fileDialog.ShowModal()
        fileDialog.Destroy()

def main():
    app = wx.App()
    TestGui(None)
    app.MainLoop()

if __name__ == '__main__':
    main()

回溯:

Traceback (most recent call last):
  File "C:\Users\matthersa\Desktop\XML-Python Testing\SSHFileDialog.py", line 103, in OnOpen
    fileDialog = SSHFileDialog(None)
  File "C:\Users\matthersa\Desktop\XML-Python Testing\SSHFileDialog.py", line 6, in __init__
    super(SSHFileDialog, self).__init__(self, parent, -1, style = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_windows.py", line 734, in __init__
    _windows_.Dialog_swiginit(self,_windows_.new_Dialog(*args, **kwargs))
TypeError: in method 'new_Dialog', expected argument 1 of type 'wxWindow *'

您可能正在使用旧教程

wx.Dialog.__init__(self,*args,**kwargs) #here you need self, as this does not pass self implicitly

super(MyDialogClass,self).__init__(*args,**kwargs) # here self is passed implicitly (eg you do not pass self as the first arg)

但是你应该小心使用 super 和 wxPython iirc 有一些基础 类 不继承自 object 这会导致 MRO 中断......( tbh 它现在可能已修复)

** TLDR; **

改变

super(SSHFileDialog, self).__init__(self, parent, -1, style = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)

super(SSHFileDialog, self).__init__( parent, -1, style = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)

在评论中回答你的其他问题

class SSHFileDialog(wx.Dialog): #, hostname = 'DefaultHost', username = 'DefaultUser', password = 'Password'

    def __init__(self, parent,host,username,password):
        self.ssh_thing = SSHClient(host,username,password)
        super(SSHFileDialog, self).__init__(self, parent, -1, style = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)

您正在将额外的 self 传递给 wx.Dialog.__init__。对 super 的调用实质上是为您创建绑定方法,因此您无需再次传递 self

super(SSHFileDialog, self).__init__(parent, -1, style = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)