1

我正在编写我的第一个桌面应用程序,我正在努力处理类实例。这个应用程序是一个使用 paramiko 的简单 ftp 程序。到目前为止我设置的是一个connection.py,它看起来像这样......

#connect.py

import user, db
import paramiko, time, os

paramiko.util.log_to_file('paramiko-log.txt')
class Connection:
    def __init__(self):
        #Call DB Functions
        database = db.Database()
        #Set Transport
        self.transport = paramiko.Transport((user.hostname, user.port))

        #User Credentials
        username = user.username
        password = user.password
        self.transport.connect(username = username, password = password)

        self.sftp = paramiko.SFTPClient.from_transport(self.transport)

        print "Set your credentials in user.py for now!"
        msg = "Connecting as: %s, on port number %d" % (user.username, user.port)
        print msg

    def disconnect(self):
        print "Closing connection..."
        self.sftp.close()
        self.transport.close()
        print "Connection closed."

很简单。连接和断开连接。这个 connect.py 文件被导入到 main.py (这是我的 gui)

#main.py

import connect
from PySide import QtCore, QtGui

class Window(QtGui.QWidget):

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        windowWidth = 550
        windowHeight = 350

        self.establishedConnection = ""

        connectButton = self.createButton("&Connect", self.conn)
        disconnectButton = self.createButton("&Disconnect", self.disconnect)
        grid = QtGui.QGridLayout()
        grid.addWidget(connectButton, 3, 3)
        grid.addWidget(disconnectButton, 4, 3)
        grid.addWidget(self.createList(), 1, 0, 1, 4)

        self.setLayout(grid)     

        self.resize(windowWidth, windowHeight)
        self.setWindowTitle("FTP Program")

    def conn(self):
        connection = connect.Connection()
        self.establishedConnection = connection

    def disconnect(self):
        self.establishedConnection.disconnect()

    def createButton(self, text, member):
        button = QtGui.QPushButton(text)
        button.clicked.connect(member)
        return button

if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    gui = Window()
    gui.show()
    sys.exit(app.exec_())

问题是断开连接。我在想__init__会创建一个Connection()类的实例。如果您查看 main.py,您会看到我尝试创建变量self.connectionEstablished以保存对象,以便稍后我可以调用 disconnect 。

我哪里错了?我对 python 和其他非网络语言相当陌生(我大部分时间都在编写 RoR 和 php 应用程序)。

任何时候都不会显示任何错误,我将这个应用程序作为终端应用程序启动,所以我知道 connect.py 确实按预期工作。

编辑:所以我猜 Senderle 收到了连接关闭消息,这也是我希望看到的,但我不是。如果我看到可以解决我的问题的东西,我会标记一个最佳答案。

编辑解决:将 connect.py 和 main.py 推入一个文件以简化事情。出于某种原因,这解决了问题。所以谁知道发生了什么。我仍然会推迟“最佳答案”。如果有人能告诉我为什么我不能有这样的拆分文件,那么我会全神贯注。

4

2 回答 2

1

I tried the code and it ran fine. I made only a few changes.

First, I didn't know what "user" and "db" are, so I commented out

import user, db

and

database = db.Database()

and used my own data for username, password, etc.

Second, the PySide module isn't available via my package manager, so I used PyQt4 instead. It didn't like grid.addWidget(self.createList(), 1, 0, 1, 4) so I commented that out, and everything worked as expected.

Further thoughts: When there were connection errors, there was some console feedback consisting of stack traces, but nothing more, and self.establishedConnection remained a string, causing self.establishedConnection.disconnect() to fail. So perhaps there's a connection problem?

EDIT: Aaaahhhhh, I just saw this: "No errors are shown at any time." Are you running this from a terminal or double-clicking an executable? If you start it from a terminal, I bet you'll see stacktraces in the terminal. The gui doesn't close when the code hits an exception.

EDIT2: If joining the files fixes the problem, then I am certain the problem cannot have anything to do with python itself. This has to be a problem with eclipse. You say that connection.py began as a terminal app, so you must be able to run python apps from the command line. Try the following: put main.py, connect.py, etc. in a directory of their own, open a terminal, and run python main.py. If it works as expected, then the problem has something to do with eclipse.

于 2011-01-21T21:05:55.183 回答
0

您没有在构造函数中调用 conn() 。

于 2011-01-21T20:43:19.020 回答