button1 = QtGui.QPushButton("This is button1", self)
# set button to call somefunction() when clicked
buton1.clicked.connect(self.somefunction)
def somefunction(self):
# this is called when button1 is clicked
# put directory specific tasks here
# examples:
ddir = QtGui.QFileDialog.getExistingDirectory(self, "Get Dir PAth")
# ddir is a QString containing the path to the directory you selected
print ddir # this will output something like 'C://path/you/selected'
# lets get a list of files from the directory:
files = [name for name in os.listdir(str(ddir))]
# txt files only:
files = [name for name in os.listdir(str(ddir)) if name.endswith('.txt')]
# jpg files only:
files = [name for name in os.listdir(str(ddir)) if name.endswith('.jpg')]
# now do something with your directory or list of files ...
不清楚你在问什么-你说的“打开一个目录”是什么意思? 您只想获取目录路径的引用吗?在
假设您的按钮在另一个类中:
相关问题 更多 >
编程相关推荐