2013-05-02 2 views
1

최근에 파이썬 연습을 시작했습니다. 특히 리눅스를 사용할 때 매우 유용합니다. 내 문제는 여기에 있습니다. Qt의 단순성과 파이썬의 힘을 정말 좋아하지만 조금 지나치게 자랐습니다. PyQt와 libtorrent를 사용하여 매우 간단한 토런트 클라이언트를 만들고 싶습니다. 그 당시에 한 급류를 다운로드하기위한 코드를 사용할 준비가되었습니다. 비록 앱 코드를 Qt 코드와 통합하는 데 문제가 있습니다. #/usr/bin/python 부분을 PyQt4 수입 QtCore, QtGui 수입 libtorrent에서 수입 SYS 로 수입 시간 LTPyQt가있는 간단한 토런트 클라이언트

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는에서 생성 된 코드 : 여기 torrent.py의 코드입니다!

#!/usr/bin/python 
# -*- coding: utf-8 -*- 

# Form implementation generated from reading ui file 'untitled.ui' 
# 
# Created: Mon Apr 29 21:13:06 2013 
#  by: PyQt4 UI code generator 4.10 
# 
# WARNING! All changes made in this file will be lost! 

from PyQt4 import QtCore, QtGui 
import sys 
import libtorrent as lt 
ses = lt.session() 
try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    def _fromUtf8(s): 
     return s 

try: 
    _encoding = QtGui.QApplication.UnicodeUTF8 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig, _encoding) 
except AttributeError: 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig) 

class Ui_Form(object): 
    def setupUi(self, Form): 
     Form.setObjectName(_fromUtf8("Form")) 
     Form.resize(640, 480) 
     self.closeb = QtGui.QPushButton(Form) 
     self.closeb.setGeometry(QtCore.QRect(520, 440, 98, 27)) 
     self.closeb.setObjectName(_fromUtf8("closeb")) 
     self.listView = QtGui.QListView(Form) 
     self.listView.setGeometry(QtCore.QRect(30, 20, 581, 261)) 
     self.listView.setObjectName(_fromUtf8("listView")) 
     self.newtorb = QtGui.QPushButton(Form) 
     self.newtorb.setGeometry(QtCore.QRect(30, 300, 98, 27)) 
     self.newtorb.setObjectName(_fromUtf8("newtorb")) 
     self.stopb = QtGui.QPushButton(Form) 
     self.stopb.setGeometry(QtCore.QRect(130, 300, 98, 27)) 
     self.stopb.setObjectName(_fromUtf8("stopb")) 
     self.pauseb = QtGui.QPushButton(Form) 
     self.pauseb.setGeometry(QtCore.QRect(230, 300, 98, 27)) 
     self.pauseb.setObjectName(_fromUtf8("pauseb")) 
     self.selectallb = QtGui.QPushButton(Form) 
     self.selectallb.setGeometry(QtCore.QRect(330, 300, 141, 27)) 
     self.selectallb.setObjectName(_fromUtf8("selectallb")) 
     #self.ses = lt.session() 
     self.retranslateUi(Form) 
     QtCore.QObject.connect(self.closeb, QtCore.SIGNAL(_fromUtf8("clicked()")), Form.close) 
     QtCore.QObject.connect(self.pauseb, QtCore.SIGNAL(_fromUtf8("clicked()")), self.pausef) 
     QtCore.QObject.connect(self.stopb, QtCore.SIGNAL(_fromUtf8("clicked()")), self.stopb.click) 
     QtCore.QObject.connect(self.newtorb, QtCore.SIGNAL(_fromUtf8("clicked()")), self.newtorb.click) 
     QtCore.QObject.connect(self.selectallb, QtCore.SIGNAL(_fromUtf8("clicked()")), self.listView.selectAll) 
     QtCore.QMetaObject.connectSlotsByName(Form) 

    def retranslateUi(self, Form): 
     Form.setWindowTitle(_translate("Form", "JKTorrent", None)) 
     self.closeb.setText(_translate("Form", "close", None)) 
     self.newtorb.setText(_translate("Form", "Dodaj newtor", None)) 
     self.stopb.setText(_translate("Form", "stop", None)) 
     self.pauseb.setText(_translate("Form", "pause", None)) 
     self.selectallb.setText(_translate("Form", "Zaznacz wszystkie", None)) 
    def pausef(self): 
     self.ses.pause() 

주요 문제는 토런트 다운로드가 작동하면 Qt 앱 창이 표시되지 않는다는 것입니다. 다른 문제는 (목록보기에 모든 데이터를 인쇄하는) 멋진 Qt 윈도우에 libtorrent 코드를 넣는 방법을 모르겠습니다. 제발, 도와주세요.

답변

0

문제는 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) 

메인 함수로부터 제거하고 MyForm__init__ 시작되는 함수에, 아마도 별개의 쓰레드로 실행되어야한다.

QListView의 문제점은 무엇입니까? 대신 QTreeWidget을 사용하는 것이 좋습니다. documentation이 도움이된다는 것을 발견했습니다.