directory tree, and list of files in PyQt5
Need to insert a directory tree, and a list of files, in the application on PyQt5, I did not find it in the documentation.
Maybe someone knows how to relize(screenshots below)
import sys
import ftplib
import easygui
from PyQt5.QtWidgets import QWidget, QGridLayout, QPushButton, QGraphicsOpacityEffect
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication, QMenuBar
from PyQt5.QtCore import QUrl, QRect
from PyQt5.QtGui import QIcon
# msg = "Введите информацию о сервере"
# fieldNames = ["IP Сервера", "Логин", "Пароль"]
# title = 'FTP'
# ftp_log = easygui.multpasswordbox(msg, title, fieldNames)
ftp = ftplib.FTP('192.168.0.1', 'Artem', '1204')
# ftp.cwd('/disk1_1')
class App(QWidget):
def __init__(self):
super().__init__()
self.grid = QGridLayout()
# ++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
self.menubar = QMenuBar(self)
self.file = self.menubar.addMenu('&Опции')
self.exitAction = QAction(QIcon('cross-exit.ico'), '&Exit', self)
self.exitAction.setShortcut('Ctrl+Q')
self.exitAction.setStatusTip('Exit application')
self.exitAction.triggered.connect(self.close)
self.file.addAction(self.exitAction)
# ++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
self.ftp_lst()
self.a = None
def ftp_lst(self):
self.file_lst = ftp.nlst()
self.len_file_lst = len(ftp.nlst())
self.setLayout(self.grid)
positions = [(i,j) for i in range(self.len_file_lst) for j in range(1)]
for position, name in zip(positions, self.file_lst):
if name == '':
continue
self.button = QPushButton(name)
self.grid.addWidget(self.button, *position)
self.button.clicked.connect(self.on_click)
self.show()
def update(self):
self.ftp_lst()
def on_click(self):
a = self.sender()
ftp.cwd(f'{ftp.pwd()}/{a.text()}')
self.update()
print(ftp.pwd())
print(a.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())```
2
1 answers
I don't have some of the modules you're working with,
so I've commented something out.
Uncomment the lines marked # #@
I don't see the overall design of your app, so insert
self.grid.addWidget(self.tree, ...)
Where you need it.
import sys
import ftplib
#@ import easygui # #@
from PyQt5 import QtCore, QtGui, QtWidgets # +++
from PyQt5.QtWidgets import QWidget, QGridLayout, QPushButton, QGraphicsOpacityEffect
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication, QMenuBar
from PyQt5.QtCore import QUrl, QRect
from PyQt5.QtGui import QIcon
# msg = "Введите информацию о сервере"
# fieldNames = ["IP Сервера", "Логин", "Пароль"]
# title = 'FTP'
# ftp_log = easygui.multpasswordbox(msg, title, fieldNames)
#@ ftp = ftplib.FTP('192.168.0.1', 'Artem', '1204') # #@
# ftp.cwd('/disk1_1')
class App(QWidget):
def __init__(self):
super().__init__()
self.grid = QGridLayout(self) # +++ self
self.menubar = QMenuBar(self)
self.file = self.menubar.addMenu('&Опции')
self.exitAction = QAction(QIcon('cross-exit.ico'), '&Exit', self)
self.exitAction.setShortcut('Ctrl+Q')
self.exitAction.setStatusTip('Exit application')
self.exitAction.triggered.connect(self.close)
self.file.addAction(self.exitAction)
#@ self.ftp_lst() # #@
self.a = None
# ++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
self.model = QtWidgets.QFileSystemModel()
self.model.setRootPath('')
self.tree = QtWidgets.QTreeView()
self.tree.setModel(self.model)
self.tree.setAnimated(False)
self.tree.setIndentation(20)
self.tree.setSortingEnabled(True)
self.grid.addWidget(self.tree, 111, 0, alignment=QtCore.Qt.AlignBottom)
# ++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
def ftp_lst(self):
self.file_lst = ftp.nlst()
self.len_file_lst = len(ftp.nlst())
# self.setLayout(self.grid)
positions = [(i,j) for i in range(self.len_file_lst) for j in range(1)]
for position, name in zip(positions, self.file_lst):
if name == '':
continue
self.button = QPushButton(name)
self.grid.addWidget(self.button, *position)
self.button.clicked.connect(self.on_click)
# ? self.show()
def update(self):
self.ftp_lst()
def on_click(self):
a = self.sender()
ftp.cwd(f'{ftp.pwd()}/{a.text()}')
self.update()
print(ftp.pwd())
print(a.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.resize(640, 480) # +
ex.show() # +++ !!!
sys.exit(app.exec_())
2
Author: S. Nick, 2020-05-08 15:12:51