我正在尝试构建一个使用SSH浏览远程文件的文件浏览器。我的GUI代码不断向我抛出一个错误,所以我甚至无法测试SSH部分(不包含在下面的代码中)。我当前的错误似乎是我的类构造函数或我调用它[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 *'
答案 0 :(得分:1)
您可能正在使用旧教程
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)
答案 1 :(得分:0)
您将额外的self
传递给wx.Dialog.__init__
。对super
的调用实质上是为您创建绑定方法,因此您不需要再次传递self
。
super(SSHFileDialog, self).__init__(parent, -1, style = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)