我一直在研究這個代碼作爲我學校項目的一部分,我似乎沒有得到任何地方。我的問題是,當我嘗試從文件中添加文本到我的QlistWidget我的程序崩潰。首先我想知道這是否會將目錄中的文件(movieDir.txt
中的每一行都是目錄)添加到QlistWidget,其次我甚至不確定是否使用了正確的窗口小部件或者它應該是QlistView。我怎樣才能做到這一點,所以listWidget中當前選定的項目在文本框中顯示細節(例如文件的大小)。將文本文件中的項目添加到pyQt5中的QlistWidget中
我也在尋找這個在啓動時完成,所以如果有人可以請告訴我在哪裏將不勝感激addItems(self)
。因爲我們被忽視在學校教這個課,所以我是新來的。
我在Windows 7上
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'input.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(764, 500)
MainWindow.setMinimumSize(QtCore.QSize(764, 353))
MainWindow.setMaximumSize(QtCore.QSize(764, 500))
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralWidget)
self.verticalLayout.setContentsMargins(11, 11, 11, 11)
self.verticalLayout.setSpacing(6)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout_2.setSpacing(6)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.pushButton_5 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_5.setObjectName("pushButton_5")
self.horizontalLayout_2.addWidget(self.pushButton_5)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.verticalLayout_4 = QtWidgets.QVBoxLayout()
self.verticalLayout_4.setContentsMargins(11, 11, 11, 11)
self.verticalLayout_4.setSpacing(6)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout_4.setSpacing(6)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.listView = QtWidgets.QListView(self.centralWidget)
self.listView.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
self.listView.setViewMode(QtWidgets.QListView.ListMode)
self.listView.setObjectName("listView")
self.horizontalLayout_4.addWidget(self.listView)
self.textEdit = QtWidgets.QTextEdit(self.centralWidget)
self.textEdit.setObjectName("textEdit")
self.horizontalLayout_4.addWidget(self.textEdit)
self.verticalLayout_4.addLayout(self.horizontalLayout_4)
self.verticalLayout.addLayout(self.verticalLayout_4)
self.horizontalSlider = QtWidgets.QSlider(self.centralWidget)
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.verticalLayout.addWidget(self.horizontalSlider)
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setContentsMargins(11, 11, 11, 11)
self.verticalLayout_2.setSpacing(6)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout.setSpacing(6)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton_3 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout.addWidget(self.pushButton_3)
self.pushButton_2 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.pushButton = QtWidgets.QPushButton(self.centralWidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout_3.setSpacing(6)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.dial = QtWidgets.QDial(self.centralWidget)
self.dial.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.dial.setMouseTracking(True)
self.dial.setObjectName("dial")
self.horizontalLayout_3.addWidget(self.dial)
self.pushButton_4 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout_3.addWidget(self.pushButton_4)
self.horizontalLayout.addLayout(self.horizontalLayout_3)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.verticalLayout.addLayout(self.verticalLayout_2)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 764, 21))
self.menuBar.setObjectName("menuBar")
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(MainWindow)
self.mainToolBar.setObjectName("mainToolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtWidgets.QStatusBar(MainWindow)
self.statusBar.setObjectName("statusBar")
MainWindow.setStatusBar(self.statusBar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton_5.clicked.connect(self.addItem)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_5.setText(_translate("MainWindow", "Show Movies"))
self.pushButton_3.setText(_translate("MainWindow", "Fast-Forward"))
self.pushButton_2.setText(_translate("MainWindow", "Pause/Play"))
self.pushButton.setText(_translate("MainWindow", "Rewind"))
self.pushButton_4.setText(_translate("MainWindow", "Full Screen"))
def addItem(self):
with open('movieDir.txt', 'r') as movieDir:
for row in movieDir:
listWidget.addItem(row)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
如果您想要實時顯示目錄,可以使用'QListView'加上'QFileSystemModel'。否則,使用'self.listWidget.addItem(QListWidgetItem(icon,movie,self.listWidget)'''。這也會顯示圖標。 – Marcus