-2

I am trying to get this: a link! in my pyqt program but it isn't working I have tryed a lot and resolved a lot of errors but with this one I don't know what to do. I am just tarting with pyqt and I do not have a lot of experience with python. And is there a way to automaticly start :(def load_project_structure) when the program starts insteat of using the temp testing button?

PyQt code:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
    <x>0</x>
    <y>0</y>
    <width>1483</width>
    <height>638</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="opnemen">
    <property name="geometry">
    <rect>
    <x>470</x>
    <y>10</y>
    <width>75</width>
    <height>23</height>
    </rect>
    </property>
    <property name="text">
    <string>Opnemen</string>
    </property>
</widget>
<widget class="QPushButton" name="import_2">
    <property name="geometry">
    <rect>
    <x>470</x>
    <y>40</y>
    <width>75</width>
    <height>23</height>
    </rect>
    </property>
    <property name="text">
    <string>Inport</string>
    </property>
</widget>
<widget class="QTreeWidget" name="FileStuckture">
    <property name="geometry">
    <rect>
    <x>10</x>
    <y>10</y>
    <width>451</width>
    <height>561</height>
    </rect>
    </property>
    <column>
    <property name="text">
    <string notr="true">1</string>
    </property>
    </column>
</widget>
<widget class="QPushButton" name="testing">
    <property name="geometry">
    <rect>
    <x>470</x>
    <y>80</y>
    <width>75</width>
    <height>23</height>
    </rect>
    </property>
    <property name="text">
    <string>testing</string>
    </property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>1483</width>
    <height>21</height>
    </rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

Python code:

import os, sys
import sounddevice as sd
from scipy.io.wavfile import write
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QTreeWidgetItem
from PyQt5.QtGui import QIcon
from pathlib import Path

qtcreator_file  = "mainwindow.ui" # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtcreator_file)

class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

        self.testing.clicked.connect(self.load_project_structure)
    startpath = Path("D:/DemoGIPhoofdmap")
    def load_project_structure(startpath, FileStuckture):
        startpathh = Path("D:/DemoGIPhoofdmap/")
        for element in os.listdir(startpathh):
            path_info = startpathh / element
            parent_itm = QTreeWidgetItem(FileStuckture, [os.path.basename(element)])
            if os.path.isdir(path_info):
                load_project_structure(path_info, parent_itm)
                parent_itm.setIcon(0, QIcon('assets/folder.ico'))
            else:
                parent_itm.setIcon(0, QIcon('assets/file.ico'))


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

Error message on line 24:

parent_itm = QTreeWidgetItem(FileStuckture, [os.path.basename(element)])
TypeError: arguments did not match any overloaded call:
QTreeWidgetItem(type: int = QTreeWidgetItem.Type): too many arguments
QTreeWidgetItem(Iterable[str], type: int = QTreeWidgetItem.Type): argument 1 has unexpected type 'bool'
QTreeWidgetItem(QTreeWidget, type: int = QTreeWidgetItem.Type): argument 1 has unexpected type 'bool'
QTreeWidgetItem(QTreeWidget, Iterable[str], type: int = QTreeWidgetItem.Type): argument 1 has unexpected type 'bool'
QTreeWidgetItem(QTreeWidget, QTreeWidgetItem, type: int = QTreeWidgetItem.Type): argument 1 has unexpected type 'bool'
QTreeWidgetItem(QTreeWidgetItem, type: int = QTreeWidgetItem.Type): argument 1 has unexpected type 'bool'
QTreeWidgetItem(QTreeWidgetItem, Iterable[str], type: int = QTreeWidgetItem.Type): argument 1 has unexpected type 'bool'
QTreeWidgetItem(QTreeWidgetItem, QTreeWidgetItem, type: int = QTreeWidgetItem.Type): argument 1 has unexpected type 'bool'
QTreeWidgetItem(QTreeWidgetItem): argument 1 has unexpected type 'bool'
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Robbe
  • 17
  • 4
  • Sorry didn't copy everything `Traceback (most recent call last): File "save location", line 24, in load_project_structure` Thats everything that comes before. – Robbe Sep 18 '19 at 09:29
  • if you want to run `load_project_structure()` at start then run `self.load_project_structure(parameters)` in `__init__` – furas Sep 18 '19 at 09:31
  • method `load_project_structure()` in class should have `self` as first parameter to correctly assign values to other variables - `def load_project_structure(self, startpath, FileStuckture):` – furas Sep 18 '19 at 09:35
  • error shows that in `QTreeWidgetItem(...)` you use wrong values - one of them has wrong type. Error als o shows you what values it can accept. Maybe because you used name `FileStuckture` in `def load_project_structure(startpath, FileStuckture):` so it creates local variable `FileStuckture` and it removes `FileStuckture` which you declared in `PyQt` - `` – furas Sep 18 '19 at 09:38
  • Oke, thanks for your great and fast response. Than I need to create a other variable than FileStruckture. I did that bud how do I define it? I took tree and replaced FileStruckture with it everywhare in the script. But now there is a error that tree isn't difined. – Robbe Sep 18 '19 at 09:49
  • in ___init___ i have now `self.load_project_structure(self, startpath, tree)` and I changed load_project_stucture to: `def load_project_structure(self, startpath, tree):` – Robbe Sep 18 '19 at 09:51
  • you are wrong - in `__init__` you have to run function with values which it will assign to variable `startpath, tree` and use inside function, don't use variables `startpath, tree` in `__init__` because they exist only inside `load_project_structure` – furas Sep 18 '19 at 09:52

1 Answers1

2

I can't run it but I think it should be

EDIT: I added self. to self.FileStuckture and to self.load_project_structure() inside def load_project_structure()

class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.load_project_structure("D:/DemoGIPhoofdmap", self.FileStuckture)


    def load_project_structure(self, startpath, tree):
        startpath = Path(startpath)
        for element in os.listdir(startpath):
            path_info = startpath / element
            parent_itm = QTreeWidgetItem(tree, [os.path.basename(element)])
            if os.path.isdir(path_info):
                self.load_project_structure(path_info, parent_itm)
                parent_itm.setIcon(0, QIcon('assets/folder.ico'))
            else:
                parent_itm.setIcon(0, QIcon('assets/file.ico'))

You should run load_project_structure in __init__ with values "D:/DemoGIPhoofdmap" and FileStuckture

self.load_project_structure("D:/DemoGIPhoofdmap", FileStuckture)

but don't use name FileStuckture in line

def load_project_structure(self, startpath, tree):

because it declares local variable FileStuckture and removes class FileStuckture which you declared in PyQt.

furas
  • 134,197
  • 12
  • 106
  • 148
  • Oke thanks, I am also trying to understand it a litle bit more. now I get this error when I run your code: `Traceback (most recent call last): File "D:\Windows\OneDrive\OneDrive - Campus Sint-Ursula Lier\Schooljaar 2019-2020\GIP\Software\Program\code.py", line 34, in window = MyWindow() File "D:\Windows\OneDrive\OneDrive - Campus Sint-Ursula Lier\Schooljaar 2019-2020\GIP\Software\Program\code.py", line 18, in __init__ self.load_project_structure("D:/DemoGIPhoofdmap", FileStuckture) NameError: name 'FileStuckture' is not defined` – Robbe Sep 18 '19 at 09:56
  • It is defined in file with XML but I don't know how later it uses it to create these objects . Maybe it is defined as part of main windows and it is `self.FileStuckture` – furas Sep 18 '19 at 10:03
  • @furas Yes - all the widgets in the xml ui file will automatically become attributes of the object passed to `setupUi` in `__init__` (i.e. `self`, in this particular case). The attribute names are taken from the names defined in the ui file. So the tree-widget will be accessible as `self.FileStuckture`. This means the second argument of `load_project_structure` isn't needed - you can just replace all usages of `tree` with `self.FileStuckture`. (Of course, it would also help if the OP used more pythonic names in the ui file - but that's another story). – ekhumoro Sep 18 '19 at 12:03
  • @ekhumoro thank you for information. I couldn't be sure if it need only `self.` or it need some method like `FileStuckture = get_by_name("FileStuckture")` which I saw in some GUI in Python - probably in `Kivy`. As for use second argument in `load_project_structure` I now saw that there is recursion and `load_project_structure` runs again `load_project_structure` with different second argument so it is still needed. – furas Sep 18 '19 at 12:29
  • @furas Ah - you're right, I completely missed that. – ekhumoro Sep 18 '19 at 12:32
  • @Robbe If this answer solved your problem, do not forget to mark this answer as correct. – S. Nick Sep 18 '19 at 12:54