Python 试图在pyqt中创建文件浏览器,但qtreewidgetitem出现错误意外bool
我正试图得到这个:!在我的pyqt程序中,但它不起作用,我尝试了很多,解决了很多错误,但对于这一个,我不知道该怎么办。 我刚刚开始使用pyqt,对python没有太多经验。 有没有一种方法可以在程序启动时自动启动:(def load_project_structure)而不是使用temp testing按钮 PyQt代码:Python 试图在pyqt中创建文件浏览器,但qtreewidgetitem出现错误意外bool,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,我正试图得到这个:!在我的pyqt程序中,但它不起作用,我尝试了很多,解决了很多错误,但对于这一个,我不知道该怎么办。 我刚刚开始使用pyqt,对python没有太多经验。 有没有一种方法可以在程序启动时自动启动:(def load_project_structure)而不是使用temp testing按钮 PyQt代码: <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Main
<?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>
第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'
我不能运行它,但我认为它应该运行 编辑:我将
self.
添加到self.filestucture
和self.load\u project\u structure()
内部def load\u project\u structure()
您应该使用值“D:/DemoGIPhoofdmap”
和filestucture运行中的load\u project\u structure
self.load_project_structure("D:/DemoGIPhoofdmap", FileStuckture)
但是不要在行中使用namefilestackture
def load_project_structure(self, startpath, tree):
因为它声明了局部变量filestackture
并删除了您在PyQt
中声明的类filestackture
,抱歉,没有复制所有内容回溯(最后一次调用):文件“保存位置”,第24行,在load\u project\u structure中,这是前面的所有内容。如果要在开始时运行load\u project\u structure()
,请在中运行self.load\u project\u structure(参数)
方法load\u project\u structure()
类中应将self
作为第一个参数,以正确地为其他变量赋值-def load\u project\u结构(self、startpath、filestucture):
错误显示在qtreewigetItem(…)
中使用了错误的值-其中一个值的类型错误。错误als o显示它可以接受的值。可能是因为您在def load_project_结构(startpath,filestucture)中使用了namefilestucture
,
所以它创建了局部变量filestucture
,并删除了您在PyQt
-Oke中声明的filestucture
,感谢您的快速响应。然后我需要创建一个除filestructure之外的其他变量。是我干的,巴德,我怎么定义它?我选择了tree,并用它替换了脚本中的所有文件结构。但现在有一个错误,树没有被定义。好的,谢谢,我也试着理解它多一点。现在我在运行代码时遇到了这个错误:Traceback(最近一次调用):文件“D:\Windows\OneDrive\OneDrive-Campus Sint Ursula Lier\Schooljaar 2019-2020\GIP\Software\Program\code.py”,第34行,在window=MyWindow()文件“D:\Windows\OneDrive\OneDrive-Campus Sint Ursula Lier\Schooljaar 2019-2020\GIP\Software\Program\code.py”中,第18行,在uuu init_uuuself.load_uproject_u结构(“D:/DemoGIPhoofdmap”,filestucture)NameError:name'filestucture'未定义
它是在带有XML的文件中定义的,但我不知道以后它如何使用它来创建这些对象。可能它被定义为主窗口的一部分,它是self.filestackture
@furas Yes-xml ui文件中的所有小部件将自动成为传递到\uuu init\uuuu
中的setupUi
的对象的属性(在这种特殊情况下,即self
)。属性名称取自ui文件中定义的名称。因此,树小部件可以作为self.filestucture
访问。这意味着不需要load\u project\u structure
的第二个参数-您只需将tree
的所有用法替换为self.filestucture
。(当然,如果OP在ui文件中使用更多的Python名称也会有所帮助——但那是另一回事了)。@ekhumoro感谢您提供的信息。我不能确定它是否只需要self.
或者它需要像filestucture=get\u by\u name(“filestucture”)
这样的方法,我在Python的一些GUI中看到了这些方法-可能是在Kivy
中看到的。至于在load\u project\u structure
中使用第二个参数,我现在看到有递归,load\u project\u structure
再次运行load\u project\u structure
,使用不同的第二个参数,所以仍然需要它。@furas Ah-你说得对,我完全错过了。
self.load_project_structure("D:/DemoGIPhoofdmap", FileStuckture)
def load_project_structure(self, startpath, tree):