我正在嘗試構建一個使用SSH瀏覽遠程位置文件的文件瀏覽器。我的GUI代碼不斷向我拋出一個或另一個錯誤,所以我甚至無法測試SSH部分(未包含在下面的代碼中)。我目前的錯誤似乎是我的類構造函數或我稱之爲[SSHFileDialog]的問題。如果有人能指出我在這裏出錯的地方,我將不勝感激。我對Python的知識是自學成才的,我最近纔開始用wxPython編寫GUI。TypeError:方法'new_Dialog'中,'wxWindow *'類型的預期參數1
代碼:
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 *'
謝謝,這已經解決了我的問題。如果你不介意回答,當我打電話給我的時候,我怎麼能把關鍵字的爭論傳遞給這個類?我最終需要傳遞SSH連接的參數。 當我將「* args,** kwargs」標記到__init__的末尾,並取消註釋下面三行,然後用'** args'將'args'傳遞給'OnOpen'類時,它會拋出一個錯誤:「 __initi __()得到了一個意想不到的關鍵字參數'username'「 –
你接受你繼承的(子類)類的附加參數...然後在那裏做一些事情...... wx.Dialog需要一組特定的參數,只有那些參數傳遞給超級調用...(即使你可以傳遞額外的參數'wx.Dialog .__ init__'不知道該怎麼處理它們.. –
看到編輯答案 –