带PyQ的简单torrent客户端

2024-06-07 16:37:47 发布

您现在位置:Python中文网/ 问答频道 /正文

最近我开始练习Python。它是非常有用的PL,尤其是在使用Linux时。 所以我的问题是:我真的很喜欢Qt的简单性和Python的强大功能,但它让我有点过度了。 我想用PyQt和libtorrent构建一个非常简单的torrent客户端。 我甚至有一个随时可以使用的代码下载一个激流。 不过,我在将app代码与Qt代码集成时遇到了问题。 这是激流.py: #!/usr/bin/python 导入系统 从PyQt4导入QtCore,QtGui 将libtorrent导入为lt 导入时间

from torrent_ui import Ui_Form, ses


class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()


    ses.listen_on(6881, 6891)
    #e = lt.bdecode(open("test.torrent", 'rb').read())
    info = lt.torrent_info("g.torrent")
    h = ses.add_torrent({'ti': info, 'save_path': './'})
    print 'starting', h.name()

    while (not h.is_seed()):
       s = h.status()

       state_str = ['queued', 'checking', 'downloading metadata', 'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
       print '\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, s.num_peers, state_str[s.state]),
       sys.stdout.flush()
       #ses.pause()

       time.sleep(1)

    print h.name(), 'complete'
    sys.exit(app.exec_())

从PyQt生成的代码:

^{pr2}$

主要问题是,当torrent下载正常时,Qt应用程序窗口不会出现。另一个问题是我不知道如何将libtorrent代码放入这个奇特的Qt窗口(将所有数据打印到列表视图中)。 请帮帮我。在


Tags: 代码nameltselfinfoappuisys
1条回答
网友
1楼 · 发布于 2024-06-07 16:37:47

您的问题是在app.exec_()之前有一些代码。窗口将显示执行该操作的时间。部分

ses.listen_on(6881, 6891)
#e = lt.bdecode(open("test.torrent", 'rb').read())
info = lt.torrent_info("g.torrent")
h = ses.add_torrent({'ti': info, 'save_path': './'})
print 'starting', h.name()

while (not h.is_seed()):
   s = h.status()

   state_str = ['queued', 'checking', 'downloading metadata', 'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
   print '\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, s.num_peers, state_str[s.state]),
   sys.stdout.flush()
   #ses.pause()

   time.sleep(1)

应该从main函数中删除,并且应该在一个单独的线程中运行,这个函数是在MyForm__init__中启动的。在

现在还不清楚QListView有什么问题。我建议您改用QTreeWidget,我发现documentation非常有用。在

相关问题 更多 >

    热门问题