Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

奇怪的错误,加上这一行就出错

奇怪的错误,加上这一行就出错
下面的代码加上self.treeview1.clicked.connect(self.on_clicked)
这一行就出错,AttributeError: 'MainWidget' object has no attribute 'treeview1'
恳请高人指点!

import sys
import os
from PyQt5 import QtCore
from PyQt5.Qt import *

class MainWidget(QWidget):
    def __init__(self, parent=None):
        super(MainWidget, self).__init__(parent)
 
        #获取系统所有文件
        self.model01 = QFileSystemModel()
        #进行筛选只显示文件夹,不显示文件和特色文件
        self.model01.setFilter(QtCore.QDir.Dirs|QtCore.QDir.NoDotAndDotDot)
        self.model01.setRootPath('')
 
        #定义创建左边窗口
        self.treeView1 = QTreeView(self)
        self.treeView1.setModel(self.model01)
        #for col in range(1, 4):
        #    self.treeView1.setColumnHidden(col, True)
        self.treeView1.setColumnHidden(2, True)
        #self.treeView1.doubleClicked.connect(self.initUI)
        self.treeview1.clicked.connect(self.on_clicked)#
 
        #定义创建右边窗口
        self.model02 = QStandardItemModel()
        #self.model02 = QDirModel()
        self.treeView2 = QTreeView(self)
        self.treeView2.setModel(self.model02)
        self.treeView2.clicked.connect(self.itemClicked)
 
        #将创建的窗口进行添加
        self.layout = QHBoxLayout()
        self.layout.addWidget(self.treeView1)
        self.layout.addWidget(self.treeView2)
        self.setLayout(self.layout)
 
 
        self.cdpath(r'e:downloadsTemp')
    def on_clicked(self, index):
        path = self.model01.fileInfo(index).absoluteFilePath()
        self.treeView2.setRootIndex(self.model02.setRootPath(path))
        
    def initUI(self, Qmodelidx):
        #每次点击清空右边窗口数据
        self.model02.clear()
        #定义一个数组存储路径下的所有文件
        PathData = []
        #获取双击后的指定路径
        filePath = self.model01.filePath(Qmodelidx)
        # List窗口文件赋值
        PathDataName = self.model02.invisibleRootItem()
        #拿到文件夹下的所有文件
        PathDataSet = os.listdir(filePath)
        #进行将拿到的数据进行排序
        PathDataSet.sort()
        #遍历判断拿到的文件是文件夹还是文件,Flase为文件,True为文件夹
        for Data in range(len(PathDataSet)):
            if os.path.isdir(filePath + '\' + PathDataSet[Data]) == False:
                PathData.append(PathDataSet[Data])
        #将拿到的所有文件放到数组中进行右边窗口赋值。
        for got in range(len(PathData)):
            gosData = QStandardItem(PathData[got])
            PathDataName.setChild(got, gosData)
    def itemClicked(self, Qmodelidx):
        text = self.model02.data(Qmodelidx)
        print(text)
        indexItem = self.model02.index(Qmodelidx.row(), 0, Qmodelidx.parent())
        #print(indexItem)
        fileName = self.model02.data(indexItem)
        print(fileName)
        filePath = self.model01.filePath(self.treeView1.currentIndex())
        #filePath = self.model01.currentItem()
        #index=self.model01.currentIndex()
        print(filePath)
        print(self.treeView1.currentIndex())
    def cdpath(self, path):
        if os.path.exists(path):
            #pass
            index = self.model01.index(str(path))
            print(path,index,self.model01.filePath(index))
            #self.treeView1.setRootIndex(self.model01.index(os.path.dirname(str(path))))
            self.treeView1.scrollTo(index)
            self.treeView1.setCurrentIndex(index)
            self.initUI(index)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWidget()
    window.resize(600, 400)
    window.show()
    sys.exit(app.exec_())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

大小写问题吧,你定义的是

treeView1

V 是 大写啊


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...