Форум сайта python.su
0
Добрый день!
Прохожу курс от Павла Гийденко “Воркшопы по Python и PyQT”
Есть два питоновских файла.
Но при попытке выполнить window2.py выходит ошибка:
dialog.py
from PyQt4.QtGui import * from PyQt4.QtCore import * class dialogClass(QDialog): def __init__(self): super(dialogClass, self).__init__() self.ly=QVBoxLayout(self) self.label = QLineEdit() self.ly.addWidget(self.label) self.ok_btn = QPushButton('OK') self.ly.addWidget(self.ok_btn) self.cancel_btn = QPushButton('Cancel') self.ly.addWidget(self.cancel_btn) self.ok_btn.clicked.connect(self.accept) self.cancel_btn.clicked.connect(self.reject) def getData(self): return dict(text=self.label.text())
from PyQt4.QtGui import * from PyQt4.QtCore import * import os import dialog path = os.path.dirname(__file__) class simpleWindows(QWidget): def __init__(self): super(simpleWindows, self).__init__() ly = QHBoxLayout() self.setLayout(ly) self.list = QListWidget() ly.addWidget(self.list) self.list.itemClicked.connect(self.updateText) self.textBrowser = QTextBrowser() ly.addWidget(self.textBrowser) self.resize(500, 400) self.fillList() def fillList(self): for f in os.listdir(path): self.list.addItem(f) def updateText(self, item): text = open(os.path.join(path,item.text())).read() self.textBrowser.setText(text) if __name__ == '__main__': app=QApplication([]) w = simpleWindows() w.show() app.exec_()
C:\Python27\python.exe C:/Users/user/PycharmProjects/project1/week2/window2.py Traceback (most recent call last): File "C:/Users/user/PycharmProjects/project1/week2/window2.py", line 25, in updateText text = open(os.path.join(path,item.text())).read() File "C:\Python27\lib\ntpath.py", line 68, in join if p_path and p_path[0] in '\\/': TypeError: 'in <string>' requires string as left operand, not QString
Отредактировано Imarden (Сен. 15, 2015 14:38:18)
Офлайн
186
В PyQt метод item.text() возвращает строку типа QString. По этому надо писать unicode(item.text()) или использвовать PySide.
Офлайн
0
Спасибо
Офлайн